0

This is my code..Just starting with mySQL. IDE is netbeans 7.2.1

    package monitordata;

    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import javax.xml.ws.Endpoint;


    public class monitorDataMF {

    public static void main(String[] args){
        Statement stmt = null;
        String dbname = null;
        String dbuser = null;
        String dbpass = null;
        String dbport = null;
        String dbIP = null;

        //pairnoume tis parametrous gia ti sindesi me ti vasi apo to properties file
        try{                
            Properties prop = new Properties();
            prop.load(new FileInputStream("mySQL.properties"));
            dbname = prop.getProperty("dbname");
            dbuser = prop.getProperty("dbuser");
            dbpass = prop.getProperty("dbpass");
            dbport = prop.getProperty("dbport");
            dbIP = prop.getProperty("dbIP");
        }catch(IOException e){e.printStackTrace();}

        //connecting with database
        try{
            Connection conn = null;
            Class.forName("com.mysql.jdbc.Driver");
            String dbUrl = "jdbc:mysql://" + dbIP + ":" + dbport + "/" + dbname;
            conn = DriverManager.getConnection(dbUrl, dbuser, dbpass);
            String createTable = "CREATE TABLE IF NOT EXISTS test"
                    + "(NAME VARCHAR(40) NOT NULL)";
            //String insertData = "INSERT INTO test "
            //        + "VALUES ('Stelios','Thwmas')";
            stmt = conn.createStatement();
            stmt.executeUpdate(createTable);
            //stmt.executeUpdate(insertData);
        }catch(ClassNotFoundException | SQLException e){}
    }
}

I execute the update, i go to Services>WebInterfaces i hit refresh and the table isn't there. I am not sure if i should executeUpdate or executeQuery

Update: This is the actual error Caught exception: Access denied for user 'root '@'localhost' (using password: YES) but i printed the pass that i sent to the connection statement and it corresponds with the pass needed for the database to start. Not sure what i am doing wrong

1 Answer 1

1

probably you will get error in execution, you did not log the exception in catch statement. Modify the code to see error. Also you must close connection properly.

   Connection conn = null;
 try{
        Class.forName("com.mysql.jdbc.Driver");
        String dbUrl = "jdbc:mysql://" + dbIP + ":" + dbport + "/" + dbname;
        conn = DriverManager.getConnection(dbUrl, dbuser, dbpass);
        String createTable = "CREATE TABLE IF NOT EXISTS test"
                + "(NAME VARCHAR(40) NOT NULL)";
        //String insertData = "INSERT INTO test "
        //        + "VALUES ('Stelios','Thwmas')";
        stmt = conn.createStatement();
        stmt.executeUpdate(createTable);
        //stmt.executeUpdate(insertData);
    }catch(ClassNotFoundException exp){
       System.err.println("Caught IOException: " + exp.getMessage()); 
    }catch( SQLException e) {
      System.err.println("Caught IOException: " + e.getMessage());
    } finally {
       if(conn != null) {
         conn.close();
       }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.