Database UPDATE operation problems

Category: Java SE
 
c20063195
2010-02-06 05:34:47

Sponsored Links
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 ("not found driver \ 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 ("An error occurred while performing a database");
exc.printStackTrace ();
}
}

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

procedure as above, a query, an update operation, the query result is correct, update abnormal

Eclispse console output is:

java.sql.SQLException: [Microsoft] [ODBC Microsoft Access Driver] Not enough arguments, expectations are one.
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)
Frankie
error occurred while performing a database
=============== ========================================
What is consciousness, ah, what parameters insufficient, looking forward to is a ah

here String sql1 = "UPDATE Student SET name = Jimmy WHERE num = 104021000";
Statement stm = con.createStatement ();
stm.executeQuery (sql1);
not see anything wrong ah,
And that Unknown Source Is not ah data source, the data source has been set up, ah, queries are correct

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


Sponsored Links

liao100
2010-02-06 05:52:55
String sql1 = "UPDATE Student SET name = Jimmy WHERE num = 104021000";

==>

String sql1 = "UPDATE Student SET name = 'Jimmy' WHERE num = 104021000";
NewWanCome1
2010-02-06 06:12:08
suggestions first sql statement in Query Analyzer Beyond written in java to write a mistake can be hard to find otherwise
binbinchou
2010-02-06 06:23:52
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";
try {
Class . forName (driver);
} catch (ClassNotFoundException exc) {
System.out.print ("not found driver \ n");
exc.printStackTrace ();
System.exit (1);
}
try {
Connection con = DriverManager.getConnection (source);
SQLWarning warn = con.getWarnings ();
while (warn! = null) {
System.out.println ("warning:" + 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 sql2 = " UPDATE Student SET name = 'Jimmy' WHERE num = 104021000 ";
Statement stm = con.createStatement ();
stm.executeUpdate (sql2);
stm = con.createStatement ();
rs = stm.executeQuery ("SELECT * FROM Student WHERE num = 104021000");
while ; (rs.next ())
System.out.println (rs.getString ("name"));
rs.close ();
con.close ();
} catch (SQLException exc) {
System.out.println ("An error occurred while performing a database");
exc.printStackTrace ();
}
}

}

result in console:
Jimmy
Jimmy

no ; problem
gl644140806
2010-02-06 06:35:28
String sql1 = "UPDATE Student SET name = Jimmy WHERE num = 104021000";

==>

String sql1 = "UPDATE Student SET name = 'Jimmy' WHERE num = 104021000";
================== ================================================== =======
into this post:
Frankie
error occurred while performing a database
java.sql.SQLException: No ResultSet was produced
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery (Unknown Source)
at Demo.main (Demo.java: 64)
========== ================================================== =====
? ? ?

I UPDATE Student SET name = 'Jimmy' WHERE num = 104021000 phrase sql statement will not Cuoa
xx306
2010-02-06 06:41:17
update statement should executeUpdate or execute to execute, you use executeQuery, this method is to return a result set, but the update does not return a result set of results, so there is this problem, change the executeUpdate like
kydkw2
2010-02-06 06:52:44
Thank you upstairs, No ResultSet was produced solve the problem, although it is not a problem
program but UPDATE Student SET name = 'Jimmy' WHERE num = 104021000 sentence after the execution, Student's name in the table content did not change, top with 104,021,000 find is "Frankie", UPDATE after the database or "Frankie"
Did UPDATE Student SET name = 'Jimmy' WHERE num = 104021000 There phrase anything wrong, I do not see how all
I did not use the Access sql Server Query Analyzer look no hair, but did not see how the phrase Cuoa
maico2009
2010-02-06 07:08:34
Thank you, has been resolved
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.