java 连接数据库mysql 的错误
代码:packageshujuku;importjava.sql.DriverManager;importjava.sql.Statement;importjava.sq...
代码:
package shujuku;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;
public class TestInsertDemo2 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306;STUDENT?useUnicode=true&characterEncoding=UTF-8";
public static final String USERNAME="root";
public static final String PASS="12345";
public static void main(String args[]) throws Exception{
Connection conn=null;
Statement stmt=null;
Class.forName(DRIVER);
String sql="INSERT INTO user(id,name,pass,age,sex) VALUE(1,'LICHENBIN','123456',22,'NAN')";
conn=DriverManager.getConnection(URL,USERNAME,PASS);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
}
错误:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "3306;STUDENT"'.
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:422)
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:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:343)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at shujuku.TestInsertDemo2.main(TestInsertDemo2.java:17)
Caused by: java.lang.NumberFormatException: For input string: "3306;STUDENT"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.mysql.jdbc.NonRegisteringDriver.port(NonRegisteringDriver.java:866)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
... 3 more
怎么破 展开
package shujuku;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;
public class TestInsertDemo2 {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306;STUDENT?useUnicode=true&characterEncoding=UTF-8";
public static final String USERNAME="root";
public static final String PASS="12345";
public static void main(String args[]) throws Exception{
Connection conn=null;
Statement stmt=null;
Class.forName(DRIVER);
String sql="INSERT INTO user(id,name,pass,age,sex) VALUE(1,'LICHENBIN','123456',22,'NAN')";
conn=DriverManager.getConnection(URL,USERNAME,PASS);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
}
错误:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "3306;STUDENT"'.
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:422)
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:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:343)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at shujuku.TestInsertDemo2.main(TestInsertDemo2.java:17)
Caused by: java.lang.NumberFormatException: For input string: "3306;STUDENT"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.mysql.jdbc.NonRegisteringDriver.port(NonRegisteringDriver.java:866)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
... 3 more
怎么破 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |