使用Java访问MySQL执行SQL语句发现。
使用executeQuery 执行DELETE后到数据库一看没有删除。
使用executeUpdate执行DELETE SQL 成功。代码如下:
//dbsql.java
public class dbsql {
public Connection _conn=null;
public Statement _stmt=null;
public DataSource _ds;
public String dbopen() {
try {
Context initCtx=new InitialContext();
_ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/connectDB");
_conn=_ds.getConnection();
_stmt = _conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
return "ok";
} catch (Exception ex) {
return ex.getMessage();
}
}
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
rs = _stmt.executeQuery(sql);
} catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public int executeQuery(String sql,byte[] b){
int ret = 0;
try {
PreparedStatement stmt = _conn.prepareStatement(sql);
//InputStream input = new ByteArrayInputStream(b);
//stmt.setBinaryStream(0, input, (int)b.length);
stmt.setBytes(1,b);
ret = stmt.executeUpdate();
} catch (SQLException e) {
System.err.println("binary:executeQuery" + e.getMessage());
}
return ret;
}
public int executeUpdate(String sql) {
int ret = 0;
try {
ret = _stmt.executeUpdate(sql);
} catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return ret;
}
public void close(){
try {
_stmt.close();
_conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
调用
dbsql db = new dbsql();
db.dbopen();
db.executeUpdate("DELETE FROM registration;"); //此处必须使用executeQuery。但是执行INSERT INTO 使用executeQuery成功
db.close();