public static Connection acquireConnection(String host,String port,String dbName,String user,String pwd) throws ClassNotFoundException,SQLException
{
Connection connection = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + host + ":" + port;
connection = DriverManager.getConnection(url,user,pwd);
connection.setCatalog(dbName);
}catch(ClassNotFoundException e){
e.printStackTrace();
throw e;
}catch(SQLException e){
e.printStackTrace();
throw e;
}
return connection;
}
Connection con = null;
con = SqlUtil.acquireConnection("localhost", "3306", "ware", "root", "root");
错误提示:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'ware'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'ware'
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)
at com.mysql.jdbc.ConnectionImpl.setCatalog(ConnectionImpl.java:5083)
at ware.SqlUtil.acquireConnection(SqlUtil.java:30)
at ware.MainWare.main(MainWare.java:79)
倚天杖
杨魅力
相关分类