文件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)应该怎么改,大家帮帮我!
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)应该怎么改,大家帮帮我!
for(int i=0;i<5;i++)
{
sql="UPDATE "+databasetable+" SET 高等数学=? WHERE 年龄>?";
prestat.setInt(1, 60);
prestat.setString(2, "25");
prestat.addBatch(sql);
}
prestat.executeBatch();
prestat=con.prepareStatement(sql);
for(int i=0;i<5;i++)
{
prestat.setInt(1, 60);
prestat.setString(2, "25");
prestat.executeUpdate();
}年龄字段是字符串吗? 字符串可以用 > 判断?另外,你这个update语句没有任何意义,做了5次同样的动作,仅仅是测试吗?
{
prestat.setInt(1, 60);
prestat.setString(2, "25");
prestat.executeUpdate();
prestat.clearParameter();
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DBConnect { /**
* @author abc
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection() throws ClassNotFoundException, SQLException {
String sql = new String("INSERT INTO emp (id, name, password) VALUES (11, 'LeoK', 'pass') ");
Class.forName("com.gbase.jdbc.Driver");
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
con = DriverManager.getConnection("jdbc:gbase://192.168.2.109:5258/user1",
"root","111111");
stmt = con.createStatement();
stmt.execute(sql); rs = stmt.executeQuery("select * from emp limit 5");
while (rs.next()) {
System.out.print("ID: "+rs.getString("id"));
System.out.print(" Name: "+rs.getString("name"));
System.out.print(" PassWord: "+rs.getString("password"));
System.out.print("\n");
}
}catch(Exception e){
e.printStackTrace();
}finally{
rs.close();
stmt.close();
con.close();
}
return null;
}
public static void main(String[] args) {
DBConnect aa = new DBConnect();
try {
aa.getConnection();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
rs=prestat.executeQuery(sql);-------------
用新的Statement来执行,之前那个已经被预先编译了.
prestat=con.prepareStatement(sql);你这句话是预编译了一个包含sql语句的prepareStatement
但是你又写了下面的语句
sql="SELECT * FROM "+databasetable;
rs=prestat.executeQuery(sql);
等于是同一个prepareStatement包含了两个不同的sql语句,这怎么行呢
你应该createStatement创造一个新的statement 再执行第二句sql,应该就OK了
在这句后+prestat=con.prepareStatement(sql);