让我抓狂的JAVA代码
代码如下:
package innerexam;
import java.sql.*;
public class SQLHelper {
private Connection conn = null;
private PreparedStatement pdstmt = null;
private ResultSet rs = null;
public SQLHelper() {
}
private String getConnectionURL() {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JavaDatabase ";
return url;
}
//初始化 Connection 对象
public void initConnection() {
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
} catch(
ClassNotFoundException ce) {
System.out.println( "类库找不到 " + ce.getMessage());
}
try {
conn = DriverManager.getConnection(getConnectionURL(), "sa ", "sa ");
}
catch (
SQLException ex) {
System.out.println( "Connection 对象创建失败 ");
}
}
//查询方法
public ResultSet queryRecord(String uname, String upwd) {
initConnection();
try {
pdstmt = conn.prepareStatement(
"select * from UserInfo where LoginName = ? and PassWord = ? ");
pdstmt.setString(1, uname);
pdstmt.setString(2, upwd);
rs = pdstmt.executeQuery();
return rs;
}
catch (SQLException ex) {
System.out.println( "查询过程中出错 " + ex.getMessage());
return null;
}
}
}
现在碰到的问题是:编译没有问题,一运行就老说Connection对象创建失败,我实在查不出哪里出了问题,调试也没用,哪位高手帮忙看看吧,先谢过了啊!
------解决方案--------------------数据库开了吗?
代码好像没错啊
------解决方案--------------------String url = "jdb