Db.java
import java.io.*;
import java.sql.*;
import java.util.Properties;
public class Db {
    private String driver;
    private String url;
    private String user;
    private String password;
    private Connection conn;
    private Statement stm;
    private ResultSet rs;
    public Db(){
        this("DBConf.properties");
    }
    public Db(String conf) {
        loadProperties(conf);
        setConn();
    }
    public Connection getConn(){
        return this.conn;
    }
  //handle the properties file to get the informations for connection
    private void loadProperties(String conf){
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(conf));        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.driver = props.getProperty("driver");
        this.url = props.getProperty("url");
        this.user = props.getProperty("user");
        this.password = props.getProperty("password");    }
    //implement the Connection
    private void setConn(){        try {
            Class.forName(driver);            this.conn = DriverManager.getConnection(url,user,password);        } catch(ClassNotFoundException classnotfoundexception) {
              classnotfoundexception.printStackTrace();
            System.err.println("db: " + classnotfoundexception.getMessage());
        } catch(SQLException sqlexception) {
            System.err.println("db.getconn(): " + sqlexception.getMessage());
        }
    }
    public void doInsert(String sql) {
        try {
            Statement statement = conn.createStatement();
            int i = stm.executeUpdate(sql);
        } catch(SQLException sqlexception) {
            System.err.println("db.executeInset:" + sqlexception.getMessage());
        }
    }
    public void doDelete(String sql) {
        try {
            stm = conn.createStatement();
            int i = stm.executeUpdate(sql);
        } catch(SQLException sqlexception) {
            System.err.println("db.executeDelete:" + sqlexception.getMessage());
        }
    }
    public void doUpdate(String sql) {
        try {
            stm = conn.createStatement();
            int i = stm.executeUpdate(sql);
        } catch(SQLException sqlexception) {
            System.err.println("db.executeUpdate:" + sqlexception.getMessage());
        }
    }
    
    public ResultSet doSelect(String sql) {
        try {
            stm = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
            rs = stm.executeQuery(sql);
        } catch(SQLException sqlexception) {
            System.err.println("db.executeQuery: " + sqlexception.getMessage());
        }
        return rs;
    }
    public static void main(String[] args){
        try{
            Db db = new Db("D:\\workspace\\bbs\\src\\DBConf.properties");
            Connection conn = db.getConn();
            if(conn != null && !conn.isClosed()) {
                System.out.println("連結成功");
                ResultSet rs = db.doSelect("select * from user");
                while(rs.next()){
                    System.out.println(rs.getString(1)+":"+rs.getString(2)+":"+rs.getString(3));
                  }
                rs.close();
                conn.close();
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }  
}DBConf.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/dao
user=root
password=123这样系统会报错:db.getconn(): Access denied for user 'root'@'localhost' (using password: YES)如果将DBConf.properties中的password去掉,改为
DBConf.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/dao
user=root
再在Db.java中private String password; 改为String password = "123";
再将 this.password = props.getProperty("password");  注释掉,
则系统会运行OK,
请问这是个啥问题啊,谢谢!!!

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【hellosun2000】截止到2008-07-05 17:05:02的历史汇总数据(不包括此帖):
    发帖的总数量:9                        发帖的总分数:230                      
    结贴的总数量:9                        结贴的总分数:230                      
    无满意结贴数:1                        无满意结贴分:20                       
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:11.11 %               无满意结分率:8.70  %                  
    敬礼!
      

  2.   

    实现上说得明白点也就是
    一个在DBConf.properties文件中定了一个配置password=123
    由Db.java类取出后赋值给Db.java类中的password后去进行DriverManager.getConnection(url,user,password)方法中就是通不过,报以下错误:
    db.getconn(): Access denied for user 'root'@'localhost' (using password: YES) 
    自已在中间用System.out.println()方法测试过password,是String类型,值也是123,不知道是哪里出了问题
    如果直接在Db.java中定义private String password="123",直接拿来用的话,就一切ok.
      

  3.   

    如果你换了一个数据库 可以说只要改动 properties文件里的账号和密码就可以了  
    再深入点的解释 就不知道了