部分代码如下:
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变量.我这样写不正确,怎么写才能正确呢?希望各位高手指点一下,非常感谢!

解决方案 »

  1.   

    stmt.executeUpdate("delete   from   shuju   where   xingming="+inputValue);
    前提是xingming是一个整形的变量或者其他非字符串变量,如果xingming字段在数据库中是string类型,则需要加上单引号
    例如:
    stmt.executeUpdate("delete   from   shuju   where   xingming='"+inputValue+"'");  
      

  2.   

    Connection con = null;
    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();
        }
    }
      

  3.   

    PS:尽量少地使用Statement,可以采用它的子类PreparedStatement,相对其父类有很多的优点。
      

  4.   

    同意楼上的.
    用PreparedStatement 预编译
      

  5.   

    1.用PreparedStatement   
    2.使用变量,然后构造sql语句
      

  6.   

    同意楼上的. 
    用PreparedStatement 预编译
      

  7.   

    stmt.executeUpdate("delete from shuju where xingming=" + inputValue);
      

  8.   

    String sql="select * from users where name='"+name+"' and id ="+idname 为String 类型的
    id 为 int 类型的 看懂没有呢?
      

  9.   

    那句sql改成:
    delete from shuju where xingming='"+inputValue+"'
    就行了