java中遇到问题:到主机 的 TCP/IP 连接失败java.net.ConnectException: Connection refused: connect 5
java编程中遇到问题:到主机的TCP/IP连接失败。java.net.ConnectException:Connectionrefused:connect这里是源代码:...
java编程中遇到问题:到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
这里是源代码:
package lx1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class shujuku {
/**
* @param args
*/
public static void main(String[] args) throws SQLException,ClassNotFoundException{
String user = "sa";
String pwd = "123456";
String myjdbc = "jdbc:sqlserver://localhost:1433;DatabaseName=Mysql";
//创建Mysql驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//创建连接会话
Connection myConnection = DriverManager.getConnection(myjdbc,user,pwd);
//创建语句块对象
Statement Myoperation =myConnection.createStatement();
//获取结果集对象
ResultSet record = Myoperation.executeQuery("SELSECT * FROM Mysql");
while (record.next()){
System.out.println(record.getString("姓名") + "," + record.getString("性别") + "," + record.getInt("年龄"));
}
try{
if (record !=null)
//关闭结果集是需要处理异常
record.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(myConnection != null)
myConnection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
} 展开
这里是源代码:
package lx1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class shujuku {
/**
* @param args
*/
public static void main(String[] args) throws SQLException,ClassNotFoundException{
String user = "sa";
String pwd = "123456";
String myjdbc = "jdbc:sqlserver://localhost:1433;DatabaseName=Mysql";
//创建Mysql驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//创建连接会话
Connection myConnection = DriverManager.getConnection(myjdbc,user,pwd);
//创建语句块对象
Statement Myoperation =myConnection.createStatement();
//获取结果集对象
ResultSet record = Myoperation.executeQuery("SELSECT * FROM Mysql");
while (record.next()){
System.out.println(record.getString("姓名") + "," + record.getString("性别") + "," + record.getInt("年龄"));
}
try{
if (record !=null)
//关闭结果集是需要处理异常
record.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(myConnection != null)
myConnection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |