早教吧作业答案频道 -->其他-->
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...的网友还看了以下:
为什么f(1-m)+f(1-m2)>0,即为f(1-m)>f(m2-1).求详解为什么f(1-m)+ 2020-03-30 …
已知函数f(x)=10x,对于实数m、n、p有f(m+n)=f(m)+f(n),f(m+n+p)= 2020-05-13 …
已知函数f(x)满足:对任意实数m,n都有f(m+n)=f(m)+f(n)-1已知函数f(x)满足 2020-05-17 …
已知定义在R上的增函数f(x)满足f(x)>0,且对于任意的m,n∈R都有f(m)•f(n)=f( 2020-06-11 …
设函数f(x)满足:①对任意实数m,n都有f(m+n)+f(m-n)=2f(m)f(n);②对任意 2020-06-12 …
若函数y=f(x)满足f(-x)=f(x),当a,b∈(-∞,0]时总有f(a)−f(b)a−b> 2020-06-27 …
如图是“二分法”解方程的流程图.在①~④处应填写的内容分别是()A.f(a)f(m)<0;a=m; 2020-07-09 …
N个一样的球,放到M个有编号的箱子里,有多少种放法?举例N=3,M=2,有4种方法:3,0,;2, 2020-07-14 …
在f(m,n)中,m、n、f(m,n)∈N*,且对任何m,n都有:(i)f(1,1)=1;(ii)f 2020-11-01 …
定义映射f:A→B,其中A={(m,n)|m,n∈R},B=R,已知对所有的有序正整数对(m,n)满 2020-12-05 …