早教吧作业答案频道 -->其他-->
java连接mysql时Communicationslinkfailurepublicstaticvoidmain(String[]args){Connectionconn=null;Statementstmt=null;ResultSetrs=null;try{Class.forName("com.mysql.jdbc.Driver");conn=DriverManager.getConnection("jdbc:mysql
题目详情
java 连接 mysql时Communications link failure
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
▼优质解答
答案和解析
把localhost换成127.0.0.1看看,看看防火墙的3306端口打开没有,检查mysql看看用的是不是不是3306.
看了java连接mysql时Com...的网友还看了以下:
写出下列算法的功能LinkListdemo(LinkListL){ListNode*q,*p;If 2020-05-17 …
同样一个题目#include#includetypedefintDataType;typedefs 2020-05-17 …
数据结构试卷三.阅读下面的算法(10分)LinkListmynote(LinkListL){//L 2020-06-12 …
.以下几组表达式中,返回值均为.T.(真)的是..以下几组表达式中,返回值均为.T.(真)的是。A 2020-06-15 …
plsql问题instr(nvl(ivstaff,decode(t.staffid,null,'- 2020-07-09 …
单循环链表L为空的判定条件是()。A.L==NULLB.L->next==LC.L->next== 2020-07-10 …
java连接mysql时Communicationslinkfailurepublicstaticv 2020-12-13 …
计算一棵二叉树的高度,请将下列算法填空完整.intHeight(Btree*t){inth1,h2; 2021-01-02 …
ls-l&>dev/null把ls-l这个正确的,错误的都输出到黑洞设备?//换成>&又是什么意思? 2021-02-05 …
voidDestroyList(LinkList&L)中有&是什么意思,而voidClearList 2021-02-05 …