String str=text.getText();
要实现查询语句:select * from biao where name=str;
应该怎么写?直接把上面那句加到java语句中不对。

解决方案 »

  1.   

    Class.forName("com.mysql.jdbc.Driver");//获取数据库驱动

    String url = "jdbc:mysql://localhost/mydb?user=root";
    Connection con = DriverManager.getConnection(url);//获取连接         Statement st = con.createStatement();//获取连接对象
             
             String sql = "select * from biao where name=" + str;
            st.executeUpdate(sql);
            
            st.close();
            con.close();
          
      

  2.   

    你的text是什么,比如是JTextField还是别的什么
    如果是在JDBC里int result_1 = 0; Statement stmt = null;//声明一个数据库操作对象 String sql = "delete from t_user where sid='" + sid + "'"; try {
    con.setAutoCommit(false);
    stmt = con.createStatement();//通过连接得到数据库
    result_1 = stmt.executeUpdate(sql);
    if (result_1 != 0) { con.commit();//删除数据成功并提交事务 }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally { try {
    stmt.close();//删除数据库对象
    } catch (SQLException e) { e.printStackTrace();
    } }
      

  3.   


    String sql = "select * from biao where name='"+str+"'";
    这样用就ok
      

  4.   

    或者用预编译Class.forName("com.mysql.jdbc.Driver");//获取数据库驱动
            
    String url = "jdbc:mysql://localhost/mydb?user=root";
    Connection con = DriverManager.getConnection(url);//获取连接String sql = "select * from biao where name=?";
    PreparedStatement pst = con.prepareStatement(sql);//获取连接对象
    pst.setString(1, str);
            
    pst.executeUpdate();
            
    pst.close();
    con.close();
      

  5.   

    String str=text.getText();
    String sql = "select * from biao where name=?";
    Connection con = ....;
    PreparedStatement preparedStatement = con.prepareStatement(sql);
    preparedStatement.setString(1, str);
    ResultSet resultSet = preparedStatement.executeQuery();
    ....