一个连接数据库的函数,调用关闭函数时出错(附代码)
import java.sql.*;
public class osconn
{
public static final String sDBDriver= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
public static final String sConnStr= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ee ";
public static final String user= "sa ";
public static final String password= " ";
Connection connect=null;
ResultSet rs=null;
Statement stmt=null;
public osconn()
{
try
{
Class.forName(sDBDriver);
}
catch (
java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
connect=DriverManager.getConnection(sConnStr,user,password);
Statement stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
System.out.println( "连接数据库成功 ");
return rs;
}
catch (
SQLException ex)
{
System.err.println(ex.getMessage());
System.out.println( "在这里发生异常啦 ");
return null;
}
}
public int executeUpdate(String sql)
{
int result=0;
try
{
connect=DriverManager.getConnection(sConnStr,user,password);
Statement stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
stmt.close();
connect.close();
return result;
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
return result;
}
}
public void closeConn(String sql)
{
try
{
stmt.close();
connect.close();
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
}
public static void main(String[] args)
{
osconn conn=new osconn();
conn.executeQuery( "select * from try ");
conn.closeConn();
}
}
编译的时候出现以下提示:
---------- javac ----------
D:\try\osconn.java:78: 无法将 osconn 中的 closeConn(java.lang.String) 应用于 ()
conn.closeConn();
^
1 错误
输出完成 (耗时: 1 秒) - 正常终止
------解决方案--------------------public void closeConn(String sql)
改为
public void closeConn()
你这个关闭函数根本不用输入sql参数。
你调用的时候用的是无参数的的closeConn函数,而你的程序没有定义public void closeConn()这样的函数