早教吧作业答案频道 -->其他-->
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...的网友还看了以下:
连字成词(英语)l u o e b s r u s r e t o s w a e s r t e 2020-05-14 …
重新排列字母,写出单词1.s,a,p,e,c,2.r,o,e,t,c,k,3.d,c,o,o,t, 2020-06-06 …
做匀加速运动的列车出站时,车头经过站台某点O时速度是1m/s,车尾经过O点时的速度是7m/s,则这 2020-06-22 …
多音字组词。塞sāi()度dù()摩mó()刨páo()sài()duó()mā()bào()号h 2020-06-28 …
重组字母为单词e,e,r,w,he,e,r,t,he,e,se,a,sw,m,s,ir,t,p,i 2020-07-09 …
英语单词填空1.时间状语:d-r-n-2.场所:b-s-s-o-f-r--e-a-t-e-t3.教 2020-07-14 …
(2008•徐汇区二模)如图所示,⊙O的半径OA=1,点M是线段OA延长线上的任意一点,⊙M与⊙O 2020-07-24 …
推断题:现有如下反应(反应条件均不给),推断物质⑴A→B+C+D⑵C+E→C+F+D⑶D+G→H⑷ 2020-07-30 …
o物体以二的m/s的速度在空7飞行,突然由于内力的作用,物体分裂成质量为9:7的两块,在这o瞬间,大 2020-12-28 …
英文单词分类填空动物类1.O()t()p()s2.k()t()e()3.l()b()t()r4.a( 2021-02-05 …