当前位置: 技术问答>java相关
这个程序怎么了,在线等待,设了setAutocommit(true),为什么不能更新数据库
来源: 互联网 发布时间:2015-06-05
本文导语: //:CreateCoffee.java //an example using to create and update . import java.sql.*; public class CreateCoffee { public static void main(String args[]) { String url = "jdbc:odbc:coffees2"; String createString; ...
//:CreateCoffee.java
//an example using to create and update .
import java.sql.*;
public class CreateCoffee {
public static void main(String args[]) {
String url = "jdbc:odbc:coffees2";
String createString;
String myLogin="yuan";
String myPassword="132440";
Connection con;
Statement stmt;
int[] a={1,102,130,4,16};
createString = "create table COFFEES " +
"(COF_NAME VARCHAR(32), " +
"SUP_ID INTEGER, " +
"PRICE FLOAT, " +
"SALES INTEGER, " +
"TOTAL INTEGER)";
//using to load driver:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
//using to create:
try {
con = DriverManager.getConnection(url, myLogin, myPassword);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(createString);
stmt.close();
con.close();
}catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
//using to update:
try{
con = DriverManager.getConnection(url, myLogin, myPassword);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 允许自动更新
con.setAutoCommit(true);
ResultSet re = stmt.executeQuery("select * from coffees2");
re.absolute(2);
re.updateInt(1,a[0]);
re.updateInt(2,a[1]);
re.updateInt(3,a[2]);
re.updateInt(4,a[3]);
re.updateInt(5,a[4]);System.out.println("love");
re.updateRow();
while(re.next()){System.out.println("love");
System.out.println(re.getString(1)+","+ re.getString(2)+","+re.getString(3)+","+re.getString(4));
}
/* String updateString = "UPDATE COFFEES " +
"SET SALES = 90 " ;
stmt.executeUpdate(updateString);System.out.println("love");*/
re.close();
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
/* try{
Thread.currentThread().sleep(5*1000);
}catch(InterruptedException e){}*/
}
}///:~
出错提示:
[Microsoft ][JDBC-ODBC ...]非法的游标状态
//an example using to create and update .
import java.sql.*;
public class CreateCoffee {
public static void main(String args[]) {
String url = "jdbc:odbc:coffees2";
String createString;
String myLogin="yuan";
String myPassword="132440";
Connection con;
Statement stmt;
int[] a={1,102,130,4,16};
createString = "create table COFFEES " +
"(COF_NAME VARCHAR(32), " +
"SUP_ID INTEGER, " +
"PRICE FLOAT, " +
"SALES INTEGER, " +
"TOTAL INTEGER)";
//using to load driver:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
//using to create:
try {
con = DriverManager.getConnection(url, myLogin, myPassword);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(createString);
stmt.close();
con.close();
}catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
//using to update:
try{
con = DriverManager.getConnection(url, myLogin, myPassword);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 允许自动更新
con.setAutoCommit(true);
ResultSet re = stmt.executeQuery("select * from coffees2");
re.absolute(2);
re.updateInt(1,a[0]);
re.updateInt(2,a[1]);
re.updateInt(3,a[2]);
re.updateInt(4,a[3]);
re.updateInt(5,a[4]);System.out.println("love");
re.updateRow();
while(re.next()){System.out.println("love");
System.out.println(re.getString(1)+","+ re.getString(2)+","+re.getString(3)+","+re.getString(4));
}
/* String updateString = "UPDATE COFFEES " +
"SET SALES = 90 " ;
stmt.executeUpdate(updateString);System.out.println("love");*/
re.close();
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
/* try{
Thread.currentThread().sleep(5*1000);
}catch(InterruptedException e){}*/
}
}///:~
出错提示:
[Microsoft ][JDBC-ODBC ...]非法的游标状态
|
1.你这个程序有几个问题,首先,你有必要每次操作都关闭连接么?(con),那样重复建立数据库连接是非常消耗系统资源的.
2.所有的连接都是默认自动提交事务的,也就是说,除非你建立了事务(setAutoCommit(false)),不然,提交是没有意义的.
3.你的第一个sql语句是create,是对数据操作的,怎么可能用上可写游标呢?
游标是用在表一级(包括视图等)操作的,我真不知道你创建一个stmt用上它干什么,实在是风马牛不相及呢!
2.所有的连接都是默认自动提交事务的,也就是说,除非你建立了事务(setAutoCommit(false)),不然,提交是没有意义的.
3.你的第一个sql语句是create,是对数据操作的,怎么可能用上可写游标呢?
游标是用在表一级(包括视图等)操作的,我真不知道你创建一个stmt用上它干什么,实在是风马牛不相及呢!