为什么用访问MS SQL Server时抛出NullPointerException? Connection确实得到了吗?看看你的JDBC连接是否有问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码如下(比较长,不过很简单,麻烦高手帮我分析):package database;import java.sql.*;public class LinkDB { String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String sConnStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="; Connection conn=null; ResultSet rs=null; Statement stmt; String role=new String(); String password=new String(); public void setSConnStr(String db){ sConnStr+=db; //动态选择一个数据库 } public void setRole(String role){ this.role=role; //动态选择一个登陆角色 } public void Link(){ //动态连接数据库方法 //根据数据库用户角色动态赋予密码 if(role.equals("guest")){ password="guest"; } else if(role.equals("salesman")){ password="salesman"; } else if(role.equals("depta")){ password="depta"; } else if(role.equals("dba")){ password="dba"; } else if(role.equals("mananger")){ password="mananger"; } else if(role.equals("sa")){ password="1980628"; } try { Class.forName(sDBDriver); conn=DriverManager.getConnection(sConnStr,role,password); stmt=conn.createStatement(); } catch(java.lang.ClassNotFoundException e) { System.err.println(e.getMessage()); } catch(java.sql.SQLException e1) { System.err.println(e1.getMessage()); } } public String getSConnStr(){ return sConnStr; } public String getRole(){ return role; } public String getPassword(){ return password; } public Statement getStmt(){ return stmt; } public void StmtClose() throws Exception{ // the method to close the Statement stmt.close(); } public void ConnClose() throws Exception{ // the method to close the Connection conn.close(); } }______________________________________________________________________________ package database;import java.sql.*;public class QueryDB { Statement stmt; ResultSet rs=null; public void setStmt(Statement stmt){ this.stmt=stmt; } public ResultSet executeQuery(String sql){ rs=null; //清空rs try { rs=stmt.executeQuery(sql); } catch(SQLException ex) { System.err.println(ex.getMessage()); } return rs; } public void StmtClose() throws Exception { // the method to close the Statement stmt.close(); } public void RsClose() throws Exception { // the method to close the ResultSet rs.close(); }} _____________________________________________________________________________import database.*;import java.sql.*;import java.io.*;public class TestDB { public static void main(String[] args) throws Exception { LinkDB LinkDB=new LinkDB(); QueryDB QueryDB=new QueryDB(); ResultSet rs=null; String queryStr="select * from MatchInfo"; LinkDB.setSConnStr("Soccer"); LinkDB.setRole("dba"); LinkDB.Link(); QueryDB.setStmt(LinkDB.getStmt()); rs=QueryDB.executeQuery(queryStr); System.out.print("Ok?"); /* while(rs.next()){ System.out.println("Ok..."); System.out.println(rs.getString("home_team_id")); } */ }} 把LinkDB.getStmt()打印出来,看看他是不是null 是null,为什么会null呢?我用在JSP可以阿。 你可以在try { Class.forName(sDBDriver); conn=DriverManager.getConnection(sConnStr,role,password); stmt=conn.createStatement();}中把conn,stmt,打印出来,看看取值还有你的申明一个对象的时候,最好和你要使用的类的名字有所区别LinkDB LinkDB=new LinkDB();应该是LinkDB linkDB=new LinkDB(); tomcat 如何获取 work目录 mainMenu到底是什么 急急急!!大家可不可以帮一下,快来不及了。。hibernate 自己写SQL 文 如何使java应用程序后台以windows服务方式启动 java里字面值单独占一个空间吗? 新io怎么实现异步? 用eclipse请进 有没有关于如何发布Entity Bean的例子? 各为兄弟,小弟落伍,不知哪有jbuilder 6 企业版下载,兄弟们帮忙!!! java的客户端除了APP还有什么? 关于双精度减法的问题,请人指点~~ 如何继承多个类
import java.sql.*;public class LinkDB {
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
String role=new String();
String password=new String();
public void setSConnStr(String db){
sConnStr+=db; //动态选择一个数据库
}
public void setRole(String role){
this.role=role; //动态选择一个登陆角色
}
public void Link(){ //动态连接数据库方法
//根据数据库用户角色动态赋予密码
if(role.equals("guest")){
password="guest";
}
else if(role.equals("salesman")){
password="salesman";
}
else if(role.equals("depta")){
password="depta";
}
else if(role.equals("dba")){
password="dba";
}
else if(role.equals("mananger")){
password="mananger";
}
else if(role.equals("sa")){
password="1980628";
}
try {
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr,role,password);
stmt=conn.createStatement();
}
catch(java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
catch(java.sql.SQLException e1) {
System.err.println(e1.getMessage());
}
}
public String getSConnStr(){
return sConnStr;
}
public String getRole(){
return role;
}
public String getPassword(){
return password;
}
public Statement getStmt(){
return stmt;
}
public void StmtClose() throws Exception{ // the method to close the Statement
stmt.close();
}
public void ConnClose() throws Exception{ // the method to close the Connection
conn.close();
}
}
______________________________________________________________________________
package database;
import java.sql.*;public class QueryDB {
Statement stmt;
ResultSet rs=null;
public void setStmt(Statement stmt){
this.stmt=stmt;
}
public ResultSet executeQuery(String sql){
rs=null; //清空rs
try {
rs=stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
public void StmtClose() throws Exception { // the method to close the Statement
stmt.close();
}
public void RsClose() throws Exception { // the method to close the ResultSet
rs.close();
}
}
_____________________________________________________________________________import database.*;
import java.sql.*;
import java.io.*;public class TestDB {
public static void main(String[] args) throws Exception {
LinkDB LinkDB=new LinkDB();
QueryDB QueryDB=new QueryDB();
ResultSet rs=null;
String queryStr="select * from MatchInfo";
LinkDB.setSConnStr("Soccer");
LinkDB.setRole("dba");
LinkDB.Link();
QueryDB.setStmt(LinkDB.getStmt());
rs=QueryDB.executeQuery(queryStr);
System.out.print("Ok?");
/*
while(rs.next()){
System.out.println("Ok...");
System.out.println(rs.getString("home_team_id"));
} */
}
}
try {
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr,role,password);
stmt=conn.createStatement();
}
中把conn,stmt,打印出来,看看取值还有你的申明一个对象的时候,最好和你要使用的类的名字有所区别
LinkDB LinkDB=new LinkDB();
应该是LinkDB linkDB=new LinkDB();