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();
}
}
}
}
请高手赐教
解决方案 »
- 关于jQuery取servlet中的List
- 买了本书,《Java程序设计教程(Java how to program)第五版》
- java初学者的几个问题,很简单的!
- JOptionPane这个类怎么用?
- 黑暗中探索_初学者_超简单问题_Method not found
- 如何去掉JFRAME里的最小化,最大化,关闭按钮?(不是散分,只给答题的人)
- 帮帮我呀,好不容易搞出一个简短的程序,但是不能实现
- (求救!)请问怎样将SDK开发的工程文件用JB(6.0)载入?
- 是否数字的判断
- 如何将一个Date类型的数据,转化为当地时间串(菜鸟紧急求救)
- Applet中如何使用对话框
- 怎样使用本机JAVA程序来启动远程机器上的程序(脚本)
应该是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没驱动