我想把JSp连接access数据库的代码改写成连接SQL2005数据库的代码,需要改哪里
这是javabean程序,需要改哪里?我改
connect = DriverManager.getConnection(sConnStr,"sa","123456");还是连不上啊
package vote;
import java.sql.*;
public class VoteConn {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:vote";
Connection connect = null;
ResultSet rs = null;
public VoteConn() {
try {
Class.forName(sDBDriver);
}
catch(
java.lang.ClassNotFoundException e) {
System.err.println( e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
connect = DriverManager.getConnection(sConnStr);
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(
SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql) {
int result = 0;
try {
connect = DriverManager.getConnection(sConnStr);
Statement stmt = connect.createStatement();
result = stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
return result;
}
}
------解决方案--------------------
3个地方要修改。另外需要下载驱动包sqljdbc.jar,放在classpath中,
1.String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
==> "com.microsoft.sqlserver.jdbc.SQLServerDriver";
2.String sConnStr = "jdbc:odbc:vote";
==> "jdbc:sqlserver://IP:1433;databaseName=DBNAME";
IP:装SQL Server的Ip。本机可以写为,localhost
DBNAME:要链接的数据库。
3。connect = DriverManager.getConnection(sConnStr);
==〉
connect = DriverManager.getConnection(sConnStr,"sa","123456");