import java.sql.*; public class TestSQLServer { public static Connection getConnection() throws SQLException, java.lang.ClassNotFoundException{ //get connection String url="jdbc:sqlserver://localhost:1433;DatabaseName=northwind"; //loading jdbc driver for mysql System.out.println("驱动程序正在加载..."); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("驱动程序加载完毕!"); String username = "sa"; String password = "sa"; //open the database connection System.out.println("opening the database connection!"); Connection con = DriverManager.getConnection(url, username, password); System.out.println(" the database connection has opened!"); return con; } public static void main(String[] args){ //String query="select * from categories"; String query1="insert categories values(10,'Hanbao','Sweet')"; String query2="insert categories values(11,'Naicha','Coffee taste')"; try{ Connection con = getConnection(); Statement stmt=con.createStatement(); stmt.executeUpdate(query1); stmt.executeUpdate(query2); stmt.close(); con.close(); System.out.println("success"); }catch(java.lang.ClassNotFoundException e){ System.out.println("ClassNotFoundException: "); System.err.println(e.getMessage()); }catch(SQLException ex){ System.out.println("SQLException: "+ex.getMessage()); } } } 编译中出现到主机TCP/IP连接失败的错误,问题原因,SQl Server2000默认没有监听1433端口检测: 用命令telnet localhost 1433提示如下:正在连接到localhost...不能打开到主机的连接, 在端口 1433: 连接失败用命令netstat -n -a,发现不能侦听1433端口解决办法:安装SP3以上版本补丁.
关于Java无法连接SQL Server 2000的问题
最新推荐文章于 2021-03-16 13:42:29 发布