java 连接数据库时出的一点小问题
import java.sql.*;/**
* 这个类用于演示 PreparedStatement类的用法
* @author 羽
*
*/
public class TestPreparedStatement {
public static void main(String[] args) {
// 驱动类com\microsoft\sqlserver\jdbc
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// url 地址
String url = "jdbc:sqlserver://localhost:1433;databaseName=JAVATest";
String userName = "sa";
String password = "sasa";
Connection conn = null;
try {
// 加载驱动类
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
// 得到一个连接
conn = DriverManager.getConnection(url, userName, password);
// 创建PreparedStatement 语句
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM student WHERE sutId=? AND stuName = ?");
// 使用setXXX 方法设置参数
pstmt.setInt(1, 1);
pstmt.setString(2, "myName");
// 执行PretaredStatement语句
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("出现SQLException 异常");
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}
}
}
// 我用的是SQL Server2008 哎
// 出现SQLException 异常
com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.DBComms.Prelogin(Unknown Source)
at com.microsoft.sqlserver.jdbc.DBComms.<init>(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestPreparedStatement.main(TestPreparedStatement.java:28)