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,
请问这是个啥问题啊,谢谢!!!
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,
请问这是个啥问题啊,谢谢!!!
解决方案 »
- JNative释放内存的问题
- 关于java代码的执行顺序
- 【竞答】写出运行结果并解释原因
- 下面的方法swap为什么无法交换实际参数的值?(王晓东版课后题)
- java 打印问题在jframe 中
- RuntimeException和Error的处理
- 关于static的用法
- 基础问题:“覆写”与“重载”之间的区别和联系?
- 那里有免费的Jcreater下载啊?(不限制日期的)
- 关于jdbc的问题,高手请进
- a[i]=Integer.parseInt(args[i]);为什么要这样呢?也可以不加Integer.parseInt的?比如我在参数列表添加整数,那摩就不用加Integer.parseInt???????????????
- 怎末还是运行不出来?
楼主【hellosun2000】截止到2008-07-05 17:05:02的历史汇总数据(不包括此帖):
发帖的总数量:9 发帖的总分数:230
结贴的总数量:9 结贴的总分数:230
无满意结贴数:1 无满意结贴分:20
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:11.11 % 无满意结分率:8.70 %
敬礼!
一个在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.
再深入点的解释 就不知道了