JDBC 中executeUpdate 的结果无法保存 10
在这个程序中我使先用了executeUpdate更新数据库,然后用executeQuery查询,得知操作有效,但是结果却无法保存到数据库,也就是只在当前的过程有效.2楼的...
在这个程序中我使先用了executeUpdate 更新数据库,然后用executeQuery 查询,得知 操作有效,但是结果却无法保存到数据库,
也 就是只在当前的过程有效.
2楼的方法不行 展开
也 就是只在当前的过程有效.
2楼的方法不行 展开
展开全部
程序代码如下,大家帮我看一下
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;
import javax.swing.JOptionPane;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class jdob {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/financedb";
private Connection connection;
private PreparedStatement statement;
private boolean isLinkSuccess; //判断与数据库连接是否成功
private String result; //操作结果,用来返回表示是否成功
public jdob()
{
isLinkSuccess = true;
result = "";
String Mysqls = "select * from userdata where isLogin = 0";
String Mysqls2 = "update userdata set isLogin=0 where userName = ";
String Mysqls3 = "update userdata set isLogin=0 where isLogin = 1";
PreparedStatement pstmt=null;
java.sql.Statement stmt=null;
java.sql.ResultSet rest=null;
java.sql.Statement stmt2=null;
java.sql.ResultSet rest2=null;
Vector<String> vec=new Vector();
//加载驱动
try
{
Class.forName(JDBC_DRIVER);
}
catch (ClassNotFoundException e)
{
isLinkSuccess = false;
JOptionPane.showMessageDialog(null, "加载驱动失败");
//e.printStackTrace();
}
//建立与数据库的连接
try
{
connection = DriverManager.getConnection(DATABASE_URL,"root","87385179");
connection.setAutoCommit(false);
stmt=connection.createStatement();
rest= stmt.executeQuery(Mysqls);
connection.commit();
System.out.println("time " + new Timestamp(System.currentTimeMillis() - 6000));
while(rest.next()) {
// if(rest.getTimestamp("lastActivetime").compareTo(new Timestamp(System.currentTimeMillis() - 6000)) ==-1)
{ System.out.println("username: "+rest.getString("userName"));
vec.add(rest.getString("userName"));
}
}
stmt.executeUpdate(Mysqls3);
rest= stmt.executeQuery(Mysqls);
System.out.println("time " + new Timestamp(System.currentTimeMillis() - 6000));
while(rest.next()) {
// if(rest.getTimestamp("lastActivetime").compareTo(new Timestamp(System.currentTimeMillis() - 6000)) ==-1)
{ System.out.println("username: "+rest.getString("userName"));
vec.add(rest.getString("userName"));
}
}
// System.out.println(Mysqls2+"'" + rest.getString("userName") + "'");
// stmt2=this.connection.createStatement();
// rest2= stmt2.executeQuery(Mysqls2+"'" + rest.getString("userName") + "'");
}
catch (SQLException e)
{
isLinkSuccess = false;
JOptionPane.showMessageDialog(null, "连接数据库失败");
//e.printStackTrace();
} finally {
if(stmt != null) {
try {
stmt.close();
connection.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] arguments) {
new jdob();
System.out.printf("success");
}
}
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;
import javax.swing.JOptionPane;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class jdob {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/financedb";
private Connection connection;
private PreparedStatement statement;
private boolean isLinkSuccess; //判断与数据库连接是否成功
private String result; //操作结果,用来返回表示是否成功
public jdob()
{
isLinkSuccess = true;
result = "";
String Mysqls = "select * from userdata where isLogin = 0";
String Mysqls2 = "update userdata set isLogin=0 where userName = ";
String Mysqls3 = "update userdata set isLogin=0 where isLogin = 1";
PreparedStatement pstmt=null;
java.sql.Statement stmt=null;
java.sql.ResultSet rest=null;
java.sql.Statement stmt2=null;
java.sql.ResultSet rest2=null;
Vector<String> vec=new Vector();
//加载驱动
try
{
Class.forName(JDBC_DRIVER);
}
catch (ClassNotFoundException e)
{
isLinkSuccess = false;
JOptionPane.showMessageDialog(null, "加载驱动失败");
//e.printStackTrace();
}
//建立与数据库的连接
try
{
connection = DriverManager.getConnection(DATABASE_URL,"root","87385179");
connection.setAutoCommit(false);
stmt=connection.createStatement();
rest= stmt.executeQuery(Mysqls);
connection.commit();
System.out.println("time " + new Timestamp(System.currentTimeMillis() - 6000));
while(rest.next()) {
// if(rest.getTimestamp("lastActivetime").compareTo(new Timestamp(System.currentTimeMillis() - 6000)) ==-1)
{ System.out.println("username: "+rest.getString("userName"));
vec.add(rest.getString("userName"));
}
}
stmt.executeUpdate(Mysqls3);
rest= stmt.executeQuery(Mysqls);
System.out.println("time " + new Timestamp(System.currentTimeMillis() - 6000));
while(rest.next()) {
// if(rest.getTimestamp("lastActivetime").compareTo(new Timestamp(System.currentTimeMillis() - 6000)) ==-1)
{ System.out.println("username: "+rest.getString("userName"));
vec.add(rest.getString("userName"));
}
}
// System.out.println(Mysqls2+"'" + rest.getString("userName") + "'");
// stmt2=this.connection.createStatement();
// rest2= stmt2.executeQuery(Mysqls2+"'" + rest.getString("userName") + "'");
}
catch (SQLException e)
{
isLinkSuccess = false;
JOptionPane.showMessageDialog(null, "连接数据库失败");
//e.printStackTrace();
} finally {
if(stmt != null) {
try {
stmt.close();
connection.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] arguments) {
new jdob();
System.out.printf("success");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用executeUpdate后你先关闭你得Statement对象或PreparedStatement对象,然后再重新建立Statement对象或PreparedStatement对象进行查询操作,这样应该能看到更新后得结果。
stmt.executeUpdate(Mysqls3); 这句后面你先关闭stmt
stmt.close();
然后在获得stmt
stmt = connection.createStatement();
stmt.executeUpdate(Mysqls3); 后同样要先关闭stmt,在重新赋值给stmt
stmt.executeUpdate(Mysqls3); 这句后面你先关闭stmt
stmt.close();
然后在获得stmt
stmt = connection.createStatement();
stmt.executeUpdate(Mysqls3); 后同样要先关闭stmt,在重新赋值给stmt
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询