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){}
executeUpdatemethod, you need to use the one without parameters. See the duplicate.