0

What I got after e.PrintStackTrace after trying the connectionThis is my code below and I get a null exception error at line 30(stmt=connect.createStatement). I don't know what I've done wrong, this is the first time I'm using JDBC. Any help? I am trying to create tables under project 1 using the JDBC connection. How would I fix this error? Does it have to do with my connection to the SQL server?

package coms363project1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTables {

    private static Connection connect = null;

    public static void main(String[] args) {

        try {

            String userName = "coms363";
            String password = "password";
            String dbServer = "jdbc:mysql://localhost:3306/project1";

            connect = DriverManager.getConnection(dbServer, userName, password);

        } catch (Exception e) {

        }

        Statement stmt = null;

        try {

            stmt = connect.createStatement();
            stmt.addBatch("CREATE TABLE students (\r\n" + "snum INT,\r\n " + "ssn INTEGER,\r\n "
                    + "name VARCHAR(10),\r\n " + "gender VARCHAR(1),\r\n " + "dob DATE,\r\n "
                    + "c_address VARCHAR(20),\r\n " + "c_phone VARCHAR(10),\r\n " + "p_addr VARCHAR(10),\r\n "
                    + "p_phone VARCHAR(10),\r\n " + "Primary key(ssn),\r\n" + "Unique(snum),\r\n" + ");");

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

        String table1 = "create Table departments(" + "code integer," + "name varchar(50)," + "phone varchar(10),"
                + "college varchar(20)," + "primary key(code)" + ")";

        try {
            stmt.executeBatch();
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (connect != null) {
                    connect.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
  • Was your "connect = DriverManager.getConnection(dbServer, userName, password);" statement successful? Try a e.printStackTrace(); statement in your catch instead of the empty statement. – Kapitany Feb 25 '21 at 10:34
  • I did that...got an SQLException error that says: Check the image inserted in the question – MR.CODER1111 Feb 25 '21 at 10:44
  • Are you able to login to SQL in cmd using same credentials ? – SRJ Feb 25 '21 at 10:48
  • Please don't post screenshots of error messages, post the actual exception message as code-formatted text in your question. – Mark Rotteveel Feb 25 '21 at 10:52
  • In the command line try to connect to your database: mysql project1 -u coms363 -p. Does it work? – Kapitany Feb 25 '21 at 10:53
  • it says mysql is not recognized as an internal or external command, operable program or batch file. I'm using windows by the way. – MR.CODER1111 Feb 25 '21 at 10:59
  • Can you also share your pom.xml file – Yusuf Şengün Feb 25 '21 at 11:09
  • I dont know what that is...I havent created it. – MR.CODER1111 Feb 25 '21 at 11:16
  • Go to your MySql directory using the cd command, the mysql.exe is in the bin dir. Here you can run mysql.exe. – Kapitany Feb 25 '21 at 11:18
  • is there an easy way to go into the MySQL directory. I've never used a windows command prompt before – MR.CODER1111 Feb 25 '21 at 11:32
  • Yes, you can copy the path of the mysql bin directory in your Windows file manager. After copying it, start cmd.exe, and you have to run this command: cd C:\Program Files\MySQL\MySQL Server 8.0\bin (or something similar). Here you can run mysql exe – Kapitany Feb 25 '21 at 11:39
  • Ir says Access denied for user 'ODBC'@'localhost' (using password: NO) – MR.CODER1111 Feb 25 '21 at 11:42
  • Then try this command: mysql project1 -u coms363 -p – Kapitany Feb 25 '21 at 11:53
  • I got this:Access denied for user 'coms363'@'localhost' (using password: YES) – MR.CODER1111 Feb 25 '21 at 12:17
  • I think the best solution for your problem is to read this topic about Access denied error: https://stackoverflow.com/questions/20353402/access-denied-for-user-testlocalhost-using-password-yes-except-root-user – Kapitany Feb 25 '21 at 12:25

0 Answers0