1

I connected the sql server to intellij idea. I want to execute the inserting query (the code is written below). But with the following code , I'm not able to insert data into the database. I am creating address book n java using intellij idea and MySQL server and taking input from user with insert query:

B.addActionListener( new ActionListener( ) {
    @Override
    public void actionPerformed(ActionEvent e) {
    try
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        DriverManager.registerDriver( DriverManager.getDriver( "jdbc:mysql://localhost:3306/mysql" ) );

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");

        Statement st = con.createStatement();
       String rs = "insert into mysql57.addressbook" +"(fname,lname,addr,phonenum,id,email)" +"values(?,?,?,?,?,?)";

        System.out.println(532 );
        PreparedStatement preparedStatement = con.prepareStatement(rs);
        preparedStatement.setString(1, TUID.getText());
        preparedStatement.setString(2, tfn.getText());
        preparedStatement.setString(3, tln.getText());
        preparedStatement.setString(4, tpn.getText());
        preparedStatement.setString(5, tad.getText());
        preparedStatement.setString(6, tmail.getText());
        System.out.println(88 );
      preparedStatement.executeUpdate(rs);
      }
    catch (Exception ex){}
5
  • Please log the exception caught and analyze/post what is the exact exception you are receiving Commented Nov 15, 2017 at 10:08
  • there is no exception, the code compiles and runs fine but the issue is that the entry doesn't get inserted in db There is only one warning "Wed Nov 15 15:41:34 IST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended." Commented Nov 15, 2017 at 10:12
  • try to close the statement and connect and then check the database. also use commit . Commented Nov 15, 2017 at 10:14
  • sorry, my catch block was empty, exception is - "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1 " Commented Nov 15, 2017 at 10:28
  • You are using the wrong executeUpdate method, you need to use the one without parameters. See the duplicate. Commented Nov 15, 2017 at 10:43

1 Answer 1

0
B.addActionListener( new ActionListener( ) {
@Override
public void actionPerformed(ActionEvent e) {
try
{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    DriverManager.registerDriver( DriverManager.getDriver( "jdbc:mysql://localhost:3306/mysql" ) );

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");

    Statement st = con.createStatement();
   String rs = "insert into mysql57.addressbook" +"(fname,lname,addr,phonenum,id,email)" +"values(?,?,?,?,?,?)";

    System.out.println(532 );
    PreparedStatement preparedStatement = con.prepareStatement(rs);
    preparedStatement.setString(1, TUID.getText());
    preparedStatement.setString(2, tfn.getText());
    preparedStatement.setString(3, tln.getText());
    preparedStatement.setString(4, tpn.getText());
    preparedStatement.setString(5, tad.getText());
    preparedStatement.setString(6, tmail.getText());
    System.out.println(88 );
  preparedStatement.executeUpdate(rs);
  }
catch (Exception ex){
//Any exception caught will be logged here. 
ex.printStackTrace();
}

Your catch block is empty, probably there is an exception but its being swallowed.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, my catch block was empty, exception is - "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1 "
Try to dont concatenate the query yourself. stackoverflow.com/questions/21207049/…. And close the connection after execute update line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.