Java 中 NumberFoundException 的解析
Resolution of NumberFoundException in Java
我是 Java 的新手,在解决 NumberFormatException.
时遇到问题
我尝试 运行 的代码是
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.*;
public class NewClass {
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306" +
"user=root&password=root";
Connection con = DriverManager.getConnection(connectionUrl);
Statement stmt = null;
ResultSet rs = null;
stmt = con.createStatement();
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
catch(NumberFormatException ne)
{
System.out.println("NumberFormatException:"+ne.toString());
}
}
}
显示错误:
SQL Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Cannot load connection class because of underlying exception:
'java.lang.NumberFormatException: For input string:
"3306user=root&password=root"'.
我该怎么办?
您的 connectionUrl 缺少 ?
以将端口与 queryString 分开:
String connectionUrl = "jdbc:mysql://localhost:3306?" +
那是因为你的连接 URL 不对,你至少应该有一个 ?在端口号之后。
无论如何,最好使用此代码来获取连接:
String connectionUrl = "jdbc:mysql://localhost:3306";
Connection con = DriverManager.getConnection(connectionUrl, "root", "root" );
还记得在 finally 块中关闭连接、结果集和语句
像这样尝试
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DB_NAME","root","root");
}
catch(Exception e)
{
System.out.println("Error in connection"+e);
}
我是 Java 的新手,在解决 NumberFormatException.
时遇到问题
我尝试 运行 的代码是
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.*;
public class NewClass {
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306" +
"user=root&password=root";
Connection con = DriverManager.getConnection(connectionUrl);
Statement stmt = null;
ResultSet rs = null;
stmt = con.createStatement();
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
catch(NumberFormatException ne)
{
System.out.println("NumberFormatException:"+ne.toString());
}
}
}
显示错误:
SQL Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "3306user=root&password=root"'.
我该怎么办?
您的 connectionUrl 缺少 ?
以将端口与 queryString 分开:
String connectionUrl = "jdbc:mysql://localhost:3306?" +
那是因为你的连接 URL 不对,你至少应该有一个 ?在端口号之后。 无论如何,最好使用此代码来获取连接:
String connectionUrl = "jdbc:mysql://localhost:3306";
Connection con = DriverManager.getConnection(connectionUrl, "root", "root" );
还记得在 finally 块中关闭连接、结果集和语句
像这样尝试
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DB_NAME","root","root");
}
catch(Exception e)
{
System.out.println("Error in connection"+e);
}