我对Class.forName("com.mysql.jdbc.Driver")
有疑问;
我想为它创建单独的SQL维护和GUI类。当我直接输入Class.forName("com.mysql.jdbc.Driver");
文件时,一切正常。当我尝试为SQL maitenance创建单独的类时,它会抛出一堆异常。
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
}
}
抛出错误:
Thu Dec 14 22:35:47 CET 2017 WARN:在没有的情况下建立SSL连接 不建议使用服务器的身份验证。根据MySQL 5.5.45 +,5.6.26 +和5.7.6+要求如果未设置显式选项,则必须默认建立SSL连接。为了合规 使用SSL的现有应用程序验证verifyServerCertificate property设置为'false'。您需要显式禁用SSL 通过设置useSSL = false,或设置useSSL = true并为其提供truststore 服务器证书验证。 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知 数据库'dbfrommethod'在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在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 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)at at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)at at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873)at at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710) 在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) 在 com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219) 在 com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014) 在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:776)at com.mysql.jdbc.JDBC4Connection。(JDBC4Connection.java:47)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在com.mysql.jdbc.Util.handleNewInstance(Util.java:425)at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)at at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) 在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)
答案 0 :(得分:0)
好的,我明白了。我犯了很多错误。我查看了我的代码并更正了它。这是工作答案
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
构造函数保持为空而不引用dataBaseName变量
public SqlMaitanance() {
}
//Methods
方法应该有局部变量
public static void createDB(String dbN){
我无法连接到不存在的DB因此我有支持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(){
}
}
抱歉格式错误。