文件data.properties:
jdbc.drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc.url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student
jdbc.uid=sa
jdbc.pwd=源码:
import java.sql.*;
import java.util.*;
import java.io.*;
public class propertiesTest {/**
 * @param args
 */
private Connection con=null;
private PreparedStatement prestat=null;
private ResultSet rs=null;
String sql;
String num,name,sex,age;
int math,collegenglish;propertiesTest(String databasetable)
{
try
{
con=getConnection();sql="UPDATE "+databasetable+" SET 高等数学=?  WHERE 年龄>?";
prestat=con.prepareStatement(sql);
for(int i=0;i<5;i++)
{
prestat.setInt(1, 60);
prestat.setString(2, "25");
prestat.executeUpdate();
}sql="SELECT * FROM "+databasetable;
rs=prestat.executeQuery(sql);System.out.println("学号      "+"姓名      "+"性别      "+"年龄      "+"高等数学       "+"英语");while(rs.next())
{
num=rs.getString(1);
name=rs.getString(2);
sex=rs.getString(3);
age=rs.getString(4);math=rs.getInt(5);
collegenglish=rs.getInt(6);System.out.println(num+""+name+""+sex+""+age+""+math+"           "+collegenglish);
}
}catch(SQLException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}}
private static Connection getConnection() throws IOException,SQLException
{
InputStream ist=new BufferedInputStream(new FileInputStream(new File("." + File.separator+"data.properties")));
Properties prop=new Properties();
prop.load(ist);
ist.close();String drivers=prop.getProperty("jdbc.drivers");
if (drivers != null) System.setProperty("jdbc.drivers", drivers);
String url=prop.getProperty("jdbc.url");
String uid=prop.getProperty("jdbc.uid");
String pwd=prop.getProperty("jdbc.pwd");return DriverManager.getConnection(url, uid, pwd);}
public static void main(String[] args) {
// TODO 自动生成方法存根propertiesTest pro=new propertiesTest("student");
}}
错误信息:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid call Statement method: {0}
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at propertiesTest.<init>(propertiesTest.java:32)
at propertiesTest.main(propertiesTest.java:76)说我非法调用Statement方法,我+了打印测试了,rs=prestat.executeQuery(sql);这个句子出现异常,大家帮帮我,应该怎么改!装了SP4补丁和JDBC SP3驱动了

解决方案 »

  1.   

    第二次调用之前先调用prestat.close();或者另外新建一个Statement
    另外
    sql="UPDATE "+databasetable+" SET 高等数学=?  WHERE 年龄>?";
    prestat=con.prepareStatement(sql);
    for(int i=0;i<5;i++)
    {
    prestat.setInt(1, 60);
    prestat.setString(2, "25");
    prestat.executeUpdate();
    }
    最好改成
    sql="UPDATE "+databasetable+" SET 高等数学=?  WHERE 年龄>?";
    prestat=con.prepareStatement(sql);
    for(int i=0;i<5;i++)
    {
    prestat.setInt(1, 60);
    prestat.setString(2, "25");
    prestat.addBatch();
    }
    prestat.executeBatch();