ConnectSQL.java编译能通过,但在解释的时候提示:SQLException:
Message:到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refus
ed: connect
程序如下:import java.sql.*;
import java.net.URL;
public class ConnectSQL{
public static void main(String[] args){
String url="jdbc:sqlserver://localhost;DatabaseName=experiment;user=sa;password=;";
String sql="insert into student(name,sex,age,address) values('tom','男',20,'河南')";
try{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(java.lang.ClassNotFoundException e){
System.out.println("Can not load Jdbc driver");
System.out.println("ClassNotFoundException:");
System.out.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println("SQLException:");
while(ex!=null){
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}
}
}
请高手赐教
Message:到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refus
ed: connect
程序如下:import java.sql.*;
import java.net.URL;
public class ConnectSQL{
public static void main(String[] args){
String url="jdbc:sqlserver://localhost;DatabaseName=experiment;user=sa;password=;";
String sql="insert into student(name,sex,age,address) values('tom','男',20,'河南')";
try{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(java.lang.ClassNotFoundException e){
System.out.println("Can not load Jdbc driver");
System.out.println("ClassNotFoundException:");
System.out.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println("SQLException:");
while(ex!=null){
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}
}
}
请高手赐教
应该是Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
这里应该指名端口号1433吧,lz试试看
如String url="jdbc:sqlserver://localhost:1433;DatabaseName=experiment;user=sa;password=;";
还有就是password=""分号应该不需要了吧最好这样写容易看:
String url="jdbc:sqlserver://localhost:1433;DatabaseName=experiment"
String user="sa";
String password="";
然后在 Connection con=DriverManager.getConnection(url,user,password);
试试吧
sqlserver没驱动