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

4

1 回答 1

1

可能你会在执行中出错,你没有在 catch 语句中记录异常。修改代码以查看错误。此外,您必须正确关闭连接。

   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();
       }
    }
于 2012-12-13T18:43:03.320 回答