-1

I have an issue with the Class.forName("com.mysql.jdbc.Driver");

I'd like to create separate class for SQL maintenance and GUI to it. When I input Class.forName("com.mysql.jdbc.Driver"); file in directly in main everything works. When I try to create separate class for SQL maitenance it is throwing bunch of exceptions.

public static void main(String[] args) {

    Connection conn = null;

    SqlMaitanance DB1 = new SqlMaitanance();
    DB1.createDB("XD");
}


//Class for SQL maitanance

public class SqlMaitanance extends Example {

//Variables

public static String dataBaseName = "DBFROMMETHOD";
public static String dbTableName;


public static ResultSetMetaData rsmd = null;
public static ResultSet rs = null;
public static Statement stmt = null;
public static Connection conn = null;
public static final String JdbcDriver = "com.mysql.jbdc.Driver";
public static final String UserName = "root";
public static final String Password = "Herflik$00121";

public static final String Url = "jdbc:mysql://localhost/" + dataBaseName;

//Constructor

public SqlMaitanance() {


    createDB(this.dataBaseName);


}

//Methods

public static void createDB(String dataBaseName){

    String crtTable = "CREATE DATABASE " + dataBaseName;
    try{


        conn = DriverManager.getConnection(Url,UserName,Password);

        stmt = conn.createStatement();
        stmt.executeUpdate(crtTable);
        rs.close();
    }
    catch(SQLException e){
    //Handle exceptions for ClassforName

    e.printStackTrace();

    }
    finally{

        try{
            if(stmt!=null)
                conn.close();
        }catch(SQLException se){
        }// do nothing
        try{
            if(conn!=null)
                conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }//end finally try
    }


}

Error thrown:

Thu Dec 14 22:35:47 CET 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'dbfrommethod' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:776) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at SqlMaitanance.createDB(SqlMaitanance.java:40) at SqlMaitanance.(SqlMaitanance.java:27) at Example.main(Example.java:24)

Kim Kern
  • 54,283
  • 17
  • 197
  • 195
Herfoo
  • 1
  • 1

1 Answers1

0

OK, I got it. I did lot of mistakes. I went through my code and correct it. Here is working answer

import javax.swing.plaf.nimbus.State;
import java.sql.*;

public class SqlMaitanance {

//Variables

public static String dataBaseName;
public static String dbTableName;


public static ResultSetMetaData rsmd = null;
public static ResultSet rs = null;
public static Statement stmt = null;
public static Connection conn = null;
public static final String JdbcDriver = "com.mysql.jbdc.Driver";
public static final String UserName = "root";
public static final String Password = "Herflik$00121";

public static String Url;

//Constructor

Constructor was left empty without reference to the dataBaseName variable

public SqlMaitanance() {


}

//Methods 

Method should have local variable

public static void createDB(String dbN){

I cannot connect to DB which is not existing hence I have shoren URL

    Url = "jdbc:mysql://localhost/";
    String crtTable = "CREATE DATABASE " + dbN;

    try{

        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(Url,UserName,Password);

        stmt = conn.createStatement();
        stmt.executeUpdate(crtTable);
        rs.close();
    }
    catch(SQLException e){
    //Handle exceptions for ClassforName

    e.printStackTrace();

    }
    catch(ClassNotFoundException e){

        e.getException();
    }
    finally{

        try{
            if(stmt!=null)
                conn.close();
        }catch(SQLException se){
        }// do nothing
        try{
            if(conn!=null)
                conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }//end finally try
    }


}

public static void createTable(){


}



}

Sorry for formatting errors.

Herfoo
  • 1
  • 1