Skip to content Skip to sidebar Skip to footer

Jdbc To Mysql Error: "table Airportdetails Doesn't Exist"

I am trying to connect to a MySQL database from a jsp page using jdbc in the backend. I have the following code: public static void insertIntoDatabase(String code,String name,Strin

Solution 1:

executeUpdate()

Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.

Currently you are trying to use this for creating a table. That's the reason why you are getting that error.

Refer to the documentation Java 6 OR Java 1.4.2 for executeUpdate

EDIT:

You should create a table using Statement

Statementst= con.createStatement();
Stringtable="Create table .......";
st.executeUpdate(table);

Solution 2:

you can put the initialize the connection and load driver at the constructor level, then in the method you can first createt check the table if it exists or create it then if it is successful, continue with the insert operation.like this:

publicclassMyBean{ 
                 String url = "jdbc:mysql://localhost:3306/test,"root","" ";

              public MyBean(){
                 try{
                   Class.forName("com.mysql.jdbc.Driver");
                   con = DriverManager.getConnection(url);
                    }catch(Exception e){
                     }
              }
            publicstaticvoid insertIntoDatabase(String code,String name,String temp,String hum,String del) {
                    Connection con = null;
                    if (del.length() == 0) {
                        del="no data";  
                    }
                    name = name.replaceAll("\\(.+?\\)", "");
                    name = name.replaceAll(" ", "_");
                    del = del.replaceAll(" ", "_");
                    System.out.println("del "+del);


                    try {
                         con = DriverManager.getConnection(url);
                    Int result =  con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS aiportdetails(code VARCHAR(50) PRIMARY KEY, " +
                                "name VARCHAR(250), temp VARCHAR(50), hum VARCHAR(50), del VARCHAR(50))");
                   if(result>0){
                         try{
                         ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
                     }catch(Exception e){

                          }finally{
                        }
               }//end if
            } catch (SQLException ex) {
                        ex.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (con != null) {
                                con.close();
                            }

                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }

Post a Comment for "Jdbc To Mysql Error: "table Airportdetails Doesn't Exist""