用myeclipse6连接mysql数据库时出现communication link failure due to underlyin
发布网友
发布时间:2022-05-25 00:37
我来回答
共2个回答
热心网友
时间:2023-11-18 11:12
import java.sql.*;
import javax.sql.*;
public class QQJDBCConnector {
Connection conn;
String url;
String user;
String pws;
public QQJDBCConnector() {
url="jdbc:mysql://localhost/数据库名?useUnicode=true&characterEncoding=utf-8";
user="用户名";
pws="用户密码";
conn=null;
}
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url,user,pws);
}
catch (Exception e) {
System.out.println("Error occurred obtaining connection");
System.out.println(e.getMessage());
}
return conn;
}
}
热心网友
时间:2023-11-18 11:13
public static Connection getvpop() {
String sDBDriver = "com.mysql.jdbc.Driver";
String db = "数据库名称";
// String user = "sales";
String user = "root";
String passwd = "111111";
String sConnStr = "jdbc:mysql://localhost:3306/" + db + "?user="
+ user + "&password=" + passwd
+ "&useUnicode=true&characterEncoding=utf-8";
// String sConnStr = "jdbc:mysql://210.74.155.106:3306/" + db + "?user="
// + user + "&password=" + passwd
// + "&useUnicode=true&characterEncoding=utf-8";
Connection conn = null;
try {
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr);
} catch (ClassNotFoundException e) {
System.err.println("数据库连接失败:***************&&&&&&&&&&&&"
+ e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}