古詩詞大全網 - 成語查詢 - JDBC 中executeUpdate 的結果無法保存

JDBC 中executeUpdate 的結果無法保存

程序代碼如下,大家幫我看壹下

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");

}

}