如何从java到sql多次更新行

时间:2013-06-08 13:38:02

标签: sql oracle jdbc

我有这部分代码

try
 {
  Connection conn=new DBconnect().con();
   Statement stmt=new DBconnect().stm(conn);
   //Statement stmt2=new DBconnect().stm(conn);
   ResultSet rs = stmt.executeQuery( "UPDATE bill SET     REMAIND_FREE_TIME="+FreeTimeN+"WHERE telephone="+tel);
// ResultSet rs2 = stmt2.executeQuery(" UPDATE bill SET REMAIND_FREE_TIME="+FreeTimeN2+"WHERE telephone="+tel2);

问题是每当我用一个更新运行它时一切都很顺利但是当我运行它们时它会出错。这就是数据库类:

package database_console;

 import frames.login;
import java.sql.*;

public class DBconnect {
Statement stmt;
ResultSet rs;
Connection conn;

public Connection con() throws ClassNotFoundException, SQLException{
Class.forName( "oracle.jdbc.OracleDriver" ) ;

    // Get a connection to the database
    conn = DriverManager.getConnection( "jdbc:oracle:thin:@83.212.240.15:1521:orcl","it21119", "a37161637" ) ;
    return conn;
}

public Statement stm(Connection conn) throws SQLException {
    // Get a statement from the connection
   stmt = conn.createStatement() ;
return stmt;
}


public void stop(Connection conn,Statement stmt, ResultSet rs) throws SQLException{

    rs.close() ;
  stmt.close() ;
  conn.close() ;
 }
}

有什么建议吗?请事先提前。(顺便说一句,我正在使用带有ojdbc6.jar的sqldeveloper)

1 个答案:

答案 0 :(得分:0)

唯一的问题可能是您传递的参数不是数据库中的Varchar2列。

更好地使用PreparedStatement并设置参数。请查看此链接,详细了解PreparedStatementhttp://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html