2016-12-01 39 views
1
class insertdata extends insert implements ActionListener 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       try{ 

        stno=t1.getText(); 
        s_name=t2.getText(); 
        s_email=t3.getText(); 
        bno=t4.getText(); 
        s_no=Integer.parseInt(stno); 
        b_no=Integer.parseInt(bno);} 
        catch(Exception e1){System.out.println(e1);} 
       System.out.println(s_name); 
       try{ 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con=DriverManager.getConnection( 
        "jdbc:mysql://localhost:3306/Library","root",""); 


        String query = " insert into Students (s_no,s_name, b_no, s_email)" 
          + " values (?, ?, ?, ?)"; 

          // create the mysql insert preparedstatement 
          PreparedStatement preparedStmt = con.prepareStatement(query); 
          preparedStmt.setInt (1, s_no); 
          preparedStmt.setString (2, s_name); 
          preparedStmt.setInt (3, b_no); 
          preparedStmt.setString(4, s_email); 
          preparedStmt.execute(); 


        con.close(); 
       }catch(Exception e1){ System.out.println(e1);} 
      } 
     } 

     insertdata i=new insertdata(); 
     ba1.addActionListener(i); 


    } 
} 

我已将此java代码连接到数据库库,并希望从用户处获取值并将它们保存在学生中。但运行此代码后保存在表Students中的值为空,而不是用户输入的值。谁能帮我??在此先感谢在java中插入sql命令

+3

使用调试器,并找出什么事 – Jens

+0

你需要提交条款 – cralfaro

+0

做尝试调试代码?并检查你的变量 –

回答

0

可能您应该使用'preparedStmt.executeUpdate()'而不是'preparedStmt.execute()'。

+0

'execute()'会工作得很好 –

0

取出后延伸插入代码的工作正常

类insertdata实现的ActionListener {公共无效的actionPerformed(ActionEvent的五)