Adding new values to mysql database no longer possible?

19
September 21, 2018, at 2:10 PM

Hi guys please consider the code below. I was using it to add new values to my table(booking) in mysql database(seat_booking).

Variables include String(id,cname,ctype,event_name) and int(seatsno).I also call a function EventName() in this code and, trust me guys it works perfectly and I am able to derive the event name from the another table(events) but the only problem arises in this method.

Guys my project is to make a seat booking GUI app so this code does work when I used it to add new events for the "Admin" mode but doesn't work here ,i.e., when I need it to store the booking values for a particular customer. Please help guys. I would really appreciate it.

private void newbookbtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    try {
        Class.forName("java.sql.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/seat_booking","root","<mypassword>");
        Statement stmt = con.createStatement();
        String query = "SELECT * FROM booking";
        ResultSet rs = stmt.executeQuery(query);
        String cname = tfcname.getText();
        int seatsno = Integer.parseInt(tfseatsno.getText());
        String evtname = EventName();
        String ctype = "Normal Customer"; long bookid = getID();
        String id = Long.toString(bookid); 
        String query1="insert into booking values('"+ id +"','"+ cname +"','"+ ctype +"','"+evtname+"',"+ seatsno+");";
        stmt.executeUpdate(query1);
        JOptionPane.showMessageDialog(null, "Booking successfull!");
        stmt.close();
        con.close();
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null,"Error encountered while booking new event!");
    }
}                  
Answer 1

You use the wrong driver class. correct is:

Class.forName("com.mysql.jdbc.Driver");

java.sql.Driver is only an Interface. For more informations see the javadoc

BTW: Learn about prepared Statements to prevent sqlinjection Do not supress error Messages. Atleast log them or put it into the message Dialog:

catch (Exception e)
{
    e.printStacktrace();
    JOptionPane.showMessageDialog(null,"Error encountered while booking new event!"+e.getMessage());
}
Answer 2

Update (for this comment):

In your catch block, comment the JOptionPane line and add the following,

catch (Exception e){
    e.printStackTrace();
    // JOptionPane.showMessageDialog(null, ...);
}

then run your application once again and update the question with the error output.

Original answer :

If you are using jdbc:mysql you need the MySQL connector.

  • Download it from mysql-connector-java-8.0.12,
  • Add the mysql-connector-java-8.0.12.jar to your project.
  • Change the driver class,

    Class.forName("com.mysql.jdbc.Driver");
    
  • Clean and build (Optional but recommended).
READ ALSO
How Sequence Matters in Laravel query builder for mysql

How Sequence Matters in Laravel query builder for mysql

The complied query has different sequence from the codeThe query works but I don't know why it works!

44
Full Text Search No Result

Full Text Search No Result

My English is Bad Sorry

37
Multiple queries executed in java in single statement

Multiple queries executed in java in single statement

Hi I was wondering if it is possible to execute something like this using JDBC as it currently provides an exception even though it is possible in the MySQL query browser

37