Connection确实得到了吗?看看你的JDBC连接是否有问题

解决方案 »

  1.   

    代码如下(比较长,不过很简单,麻烦高手帮我分析):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")); 
    } */

    }
    }


      

  2.   

    把LinkDB.getStmt()打印出来,看看他是不是null
      

  3.   

    是null,为什么会null呢?我用在JSP可以阿。
      

  4.   

    你可以在
    try {
    Class.forName(sDBDriver);
    conn=DriverManager.getConnection(sConnStr,role,password);
    stmt=conn.createStatement();
    }
    中把conn,stmt,打印出来,看看取值还有你的申明一个对象的时候,最好和你要使用的类的名字有所区别
    LinkDB LinkDB=new LinkDB();
    应该是LinkDB linkDB=new LinkDB();