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();
            }
        }

    }

}

0 Answers0