部分代码如下:
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=F:\\sheji\\db1.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(strurl) ;
Statement stmt=con1.createStatement();
stmt.executeUpdate("delete from shuju where xingming=inputValue");
其中inputValue是一个String变量.我这样写不正确,怎么写才能正确呢?希望各位高手指点一下,非常感谢!
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=F:\\sheji\\db1.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(strurl) ;
Statement stmt=con1.createStatement();
stmt.executeUpdate("delete from shuju where xingming=inputValue");
其中inputValue是一个String变量.我这样写不正确,怎么写才能正确呢?希望各位高手指点一下,非常感谢!
前提是xingming是一个整形的变量或者其他非字符串变量,如果xingming字段在数据库中是string类型,则需要加上单引号
例如:
stmt.executeUpdate("delete from shuju where xingming='"+inputValue+"'");
PreparedStatement ps = null;
try {
...
con = DriverManager.getConnection(strurl);
String sql = "DELETE FROM shuju WHERE xingming = ?";
ps = con.prepareStatement(sql);
ps.setString(1, inputValue); // 第一个问号就是1,依次类推
ps.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception ex) {
ex.printStackTrace();
}finally{
try {
ps.close();
con.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
用PreparedStatement 预编译
2.使用变量,然后构造sql语句
用PreparedStatement 预编译
id 为 int 类型的 看懂没有呢?
delete from shuju where xingming='"+inputValue+"'
就行了