MySql 数据库连接报错
安装后修改密码为:123456,用户名为:root。
连接数据库代码:
import java.sql.*;
public class ConnectionProvider{
private String JDBC_DRIVER;
private String DB_URL;
private String DB_USER;
private String DB_PASSWORD;
public ConnectionProvider() {
JDBC_DRIVER=PropertyReader.get("JDBC_DRIVER");
DB_URL=PropertyReader.get("DB_URL");
DB_USER=PropertyReader.get("DB_USER");
DB_PASSWORD=PropertyReader.get("DB_PASSWORD");
try{
Class jdbcDriver=Class.forName(JDBC_DRIVER);
java.sql.DriverManager.registerDriver((Driver)jdbcDriver.newInstance());
}catch(Exception e){e.printStackTrace();}
}
public Connection getConnection()throws SQLException{
Connection con=java.sql.DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
return con;
}
}
import java.util.*;
import java.io.*;
public class PropertyReader {
static private Properties ps;
static{
ps=new Properties();
try{
InputStream in=PropertyReader.class.getResourceAsStream("db.txt");
ps.load(in);
in.close();
}catch(Exception e){e.printStackTrace();}
}
public static String get(String key){
return (String)ps.get(key);
}
}
db.txt 文件如下:
JDBC_DRIVER = com.mysql.jdbc.Driver
DB_URL = jdbc:mysql://localhost:3306/STOREDB?useUnicode=true&characterEncoding=GB2312
DB_USER = root
DB_PASSWORD =123456
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:812)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3269)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1182)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2670)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at sql.ConnectionProvider.getConnection(ConnectionProvider.java:22)
at sql.DBTester2.addCustomer(DBTester2.java:13)
at sql.DBTester2.main(DBTester2.java:87)
------解决方案--------------------
Connection con=java.sql.DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
断点调试中看一下 这三个变量的内容是什么。 特别是有无多余的空格。
------解决方案--------------------用客户端试试是不是mysql的问题
------解决方案--------------------字符集设置的问题。
http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
MySQL 中文显示乱码