【问题标题】:How to use java variable to insert values to mysql table?如何使用java变量将值插入mysql表?
【发布时间】:2013-06-16 22:09:17
【问题描述】:

您好,我正在尝试将值插入到 mysql 表中。我正在尝试这段代码。 我已经为变量赋值,我想将该变量传递给该插入语句。 这是正确的吗?

code
    int tspent = "1";
    String pid = "trng";
    String tid = "2.3.4";
    String rid = "tup";
    String des = " polish my shoes!";

    INSERT INTO `time_entry`(pid,tid,rid,tspend,description) VALUE ('"+pid+"','"+tid+"','"+rid+"',"+tspent+",'"+des+"');

这是我尝试过的,但我无法插入值

try
       {
           conn=DBMgr.openConnection();     
           String sqlQuery = "INSERT INTO `time_entry`(pid,tid,rid,tspend,description) VALUE ('"+pid+"','"+tid+"','"+rid+"',"+tspent+",'"+des+"');";
           st = conn.createStatement();
           rs = st.executeQuery(sqlQuery); 
       }

【问题讨论】:

  • “Java 执行 SQL”返回大量命中 :)
  • 你有什么异常吗
  • @Sam... 很明显,这段代码甚至无法编译。
  • 将 executeQuery 更改为 executeUpdate

标签: java mysql sql


【解决方案1】:

只要您的查询是SQL 数据操作语言 语句,您就应该使用executeUpdate() 方法。此外,您当前的查询容易受到SQL Injection 的攻击。

你应该使用PreparedStatement:

PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `time_entry`(pid,tid,rid,tspend,description) VALUES (?, ?, ?, ?, ?)");\

然后在这些索引处设置变量:

pstmt.setString(1, pid);
// Similarly for the remaining 4 

// And then do an executeUpdate
pstmt.executeUpdate();

【讨论】:

    【解决方案2】:

    试试这个,

        String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/dbname";
        String uname="username";
        String pass="password";
        Class.forName(driver);
        Connection c=(Connection) DriverManager.getConnection(url,uname,pass);
        Statement s=c.createStatement();
        s.executeUpdate("INSERT INTO `time_entry`(pid,tid,rid,tspend,description) VALUE ('"+pid+"','"+tid+"','"+rid+"',"+tspent+",'"+des+"')");
    

    【讨论】:

    • 你拿走所有的字符串。整数数据类型变量是否可行?
    【解决方案3】:

    使用PreparedStatement 并使用其setXXX() 方法设置值。

    PreparedStatement pstmt = con.prepareStatement("INSERT INTO `time_entry`
            (pid,tid,rid,tspend,description) VALUE 
            (?,?,?,?,?)");
    pstmt.setString(1, pid );
    pstmt.setString(2, tid);
    pstmt.setString(3, rid);
    pstmt.setInt(4, tspent);
    pstmt.setString(5,des );
    pstmt.executeUpdate();
    

    【讨论】:

      【解决方案4】:
      import java.sql.*;  
      class Adbs1{  
      public static void main(String args[]){  
      try{  
      Class.forName("com.mysql.jdbc.Driver");  
      Connection con=DriverManager.getConnection(  
      "jdbc:mysql://localhost:3306/rk","root","@dmin");  
      //here rk is database name, root is username and password  
      Statement stmt=con.createStatement();  
      
      stmt.executeUpdate("insert into emp values('rk11','Irfan')");
       // stmt.executeUpdate("delete from  emp where eid ='rk4'");
      //stmt.executeUpdate("update emp set ename='sallu bhai' where eid='rk5'");
      
       ResultSet rs=stmt.executeQuery("select * from emp");  
         while(rs.next())  
          System.out.println(rs.getString(1)+"  "+rs.getString(2));  
      
      con.close();  
            }catch(Exception e){ System.out.println(e);}  
          }  
      }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-16
        相关资源
        最近更新 更多