日期:2014-05-20  浏览次数:20928 次

数据库UPDATE操作问题
import   java.io.*;
import   java.sql.*;
public   class   Demo   {
public   static   void   main(String[]   args)   {

String   driver= "sun.jdbc.odbc.JdbcOdbcDriver ";
String   source= "jdbc:odbc:Grade ";
try{
Class.forName(driver);
}catch(ClassNotFoundException   exc){
System.out.print( "没有发现驱动\n ");
exc.printStackTrace();
System.exit(1);
}
try{
Connection   con=DriverManager.getConnection(source);
SQLWarning   warn=con.getWarnings();
while(warn!=null){
System.out.println(warn.getMessage());
warn=warn.getNextWarning();
}
String   sql= "SELECT   *   FROM   Student   WHERE   num=? ";
PreparedStatement   pStm=con.prepareStatement(sql);
  pStm.setInt(1,   104021000);
  ResultSet   rs=pStm.executeQuery();
  while(rs.next())
  System.out.println(rs.getString( "name "));
        String   sql1= "UPDATE   Student   SET   name=Jimmy   WHERE   num=104021000 ";
Statement   stm=con.createStatement();
        stm.executeQuery(sql1);
}catch(SQLException   exc){
System.out.println( "在执行数据库时发生错误 ");
exc.printStackTrace();
}
}

}
==============================================================


程序如上,一个查询操作,一个更新操作,查询结果正确,更新出现异常

Eclispse控制台输出为:

java.sql.SQLException:   [Microsoft][ODBC   Microsoft   Access   Driver]   参数不足,期待是   1。
at   sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown   Source)
at   sun.jdbc.odbc.JdbcOdbc.standardError(Unknown   Source)
at   sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown   Source)
at   sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown   Source)
at   sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown   Source)
at   Demo.main(Demo.java:64)
林文龙
在执行数据库时发生错误
=======================================================
这是什么意识啊,什么   参数不足,期待是   1啊

这里   String   sql1= "UPDATE   Student   SET   name=Jimmy   WHERE   num=104021000 ";
Statement   stm=con.createStatement();
        stm.executeQuery(sql1);
看不出什么错误啊,
还有那个Unknown   Source是不是数据源啊,数据源已经设置了啊,查询都正确的

?????????????????????



------解决方案--------------------
String sql1= "UPDATE Student SET name=Jimmy WHERE num=104021000 ";

==>

String sql1= "UPDATE Student SET name= 'Jimmy ' WHERE num=104021000 ";
------解决方案--------------------
建议先把sql语句在查询分析器里写好 再往java里写 不然出错了可不好找
------解决方案--------------------
关注
------解决方案--------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

public class Demo {
public static void main(String[] args) {

String driver = "sun.jdbc.odbc.JdbcOdbcDriver ";
String source = "jdbc:odbc:Grade ";