import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
public class Jdbctest5 {
private Connection con; //与数据库连接
private String url; //连接路径
private String serverName; //服务器名称
private String portNumber; //端口号
private String databaseName; //数据库名称
private String userName; //用户名
private String password; //密码
private String sql; //查询语句
Jdbctest5() {
url = "jdbc:microsoft:sqlserver://";
serverName = "localhost";
portNumber = "1433";
databaseName = "Jtest";
userName = "Ja";
password = "123";
}
private String getConnectionUrl() {
return url + serverName + ":" + portNumber +
";databaseNamme=" +
databaseName + ";";
}
private Connection getConnection() {
try {
Class.forName("com.miscroft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(getConnectionUrl(),
userName, password);
if (con != null) {
System.out.println("连接成功");
}
else
{
System.out.println("连接失败");
}
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("getConnection()内的跟踪错误:"
+ ex.getMessage());
}
return con;
}
public void display() {
try {
con = getConnection();
PreparedStatement pstmt = con.prepareStatement(
"update friends set salay=? where name like ?");
pstmt.setInt(1, 10000);
pstmt.setString(2, "李四");
pstmt.executeUpdate();
System.out.println("记录已更新");
Statement s = con.createStatement();
String sql = "select * from friends";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
System.out.println(" ");
System.out.print(rs.getInt(1) + " ");
System.out.println(rs.getInt(5));
}
}
catch (SQLException ex) {
System.out.println(ex);
}
}
public static void main(String[] args) {
Jdbctest5 obj = new Jdbctest5();
obj.display();
}
}
1.以上这段红色代码加入后,报错:getConnection()内的跟踪错误:com.miscroft.jdbc.sqlserver.SQLServerDriver
不加入就没任何提示
2.是否需要导入msbase.jar,mssqlserver.jar,msutil.jar这三个包,如何导入?