import java.sql.*;public class DBConnection {private static Connection con;public static Connection getMySQLConnection() {try {Class.forName("com.mysql.jdbc.Driver");Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ray", "root", "root");}catch(ClassNotFoundException e) {System.out.println("Sorry, you get a ClassNotFoundException");}catch(SQLException e) {System.out.println("Sorry, you get a SQLException");}if (con == null) {System.out.println("con is null");}return con;}}
2 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
private static Connection con; 这里定义了一个con
public static Connection getMySQLConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
这里又定义了一个con,它在try的范围内有效,出了try就没效了。
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/ray", "root", "root");
- 2 回答
- 0 关注
- 571 浏览
添加回答
举报
0/150
提交
取消