我在eclipse中写了个简单的java连接mysql数据库的程序,总连不上,求指导!
importjava.sql.*;publicclassJdbcSql{publicstaticvoidmain(Stringargs[])throwsException...
import java.sql.*;
public class JdbcSql {
public static void main(String args[]) throws Exception{
String Driver="com.mysql.jdbc.Driver" ; //驱动程序
String URL="jdbc:mysql://localhost:3306/forjava" ; //连接的URL,db_name为数据库名
String Username="***" ; //用户名
String Password="***" ; //密码
Class.forName(Driver);
Connection con=DriverManager.getConnection(URL,Username,Password);
}
}
我有导入mysql-connector-java-5.1.18-bin.jar
报错为:
Exception in thread "main" java.sql.SQLException: Access denied for user 'MySQL'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JdbcSql.main(JdbcSql.java:10) 展开
public class JdbcSql {
public static void main(String args[]) throws Exception{
String Driver="com.mysql.jdbc.Driver" ; //驱动程序
String URL="jdbc:mysql://localhost:3306/forjava" ; //连接的URL,db_name为数据库名
String Username="***" ; //用户名
String Password="***" ; //密码
Class.forName(Driver);
Connection con=DriverManager.getConnection(URL,Username,Password);
}
}
我有导入mysql-connector-java-5.1.18-bin.jar
报错为:
Exception in thread "main" java.sql.SQLException: Access denied for user 'MySQL'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JdbcSql.main(JdbcSql.java:10) 展开
展开全部
这个问题,我有经验。程序没有错的情况下。
先停止mysql服务,然后打开命令行。输入mysqld_safe --skip-grant-tables
然后输入 mysql -u root
然后修改密码update user set Password = PASSWORD('新密码') where User ='root';
当然,密码也可以不改
然后flus privileges接着quit退出
然后重启mysql。
搞定
先停止mysql服务,然后打开命令行。输入mysqld_safe --skip-grant-tables
然后输入 mysql -u root
然后修改密码update user set Password = PASSWORD('新密码') where User ='root';
当然,密码也可以不改
然后flus privileges接着quit退出
然后重启mysql。
搞定
追问
貌似我在命令行里输哪些东西都没反应,
我进了mysql界面 输了密码后再输其他的东西都没用了 连quit都没反应
还有就是 程序里的user 和 password 是不是装mysql是设置的啊?我记得都没错啊
不知怎么就是连不上。。。
展开全部
我对这个也不是很熟悉,我从网上找的一个,我用了连接成功!你可以参考下。
import java.sql.*;
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhaoshu?useUnicode=true&characterEncoding=UTF-8&user=root&password=");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
import java.sql.*;
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhaoshu?useUnicode=true&characterEncoding=UTF-8&user=root&password=");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package com.geo.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MobileBankDao {
private Connection conn = null;
private Statement st = null;
private String url="localhost";
private String port="3306";
private String user = "root";
private String password = "root";
private String sDBDriver = "org.gjt.mm.mysql.Driver";
private String db="mobilebank";
public MobileBankDao() {
try {
Class.forName(sDBDriver);
getConnection();
} catch (java.lang.ClassNotFoundException e) {
System.err.println("lib下驱动包未安装");
}
}
// 创建连接
private void getConnection() {
// 采用gb2312字符集
String sConnStr = "jdbc:mysql://"+url+":"+port+"/"+db+"?useServerPrepStmts=false&useUnicode=true&characterEncoding=gb2312";
System.out.println(sConnStr);
try {
conn = DriverManager.getConnection(sConnStr, user, password);
} catch (SQLException ex) {
System.err.println("connect对象建立错误!");
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MobileBankDao {
private Connection conn = null;
private Statement st = null;
private String url="localhost";
private String port="3306";
private String user = "root";
private String password = "root";
private String sDBDriver = "org.gjt.mm.mysql.Driver";
private String db="mobilebank";
public MobileBankDao() {
try {
Class.forName(sDBDriver);
getConnection();
} catch (java.lang.ClassNotFoundException e) {
System.err.println("lib下驱动包未安装");
}
}
// 创建连接
private void getConnection() {
// 采用gb2312字符集
String sConnStr = "jdbc:mysql://"+url+":"+port+"/"+db+"?useServerPrepStmts=false&useUnicode=true&characterEncoding=gb2312";
System.out.println(sConnStr);
try {
conn = DriverManager.getConnection(sConnStr, user, password);
} catch (SQLException ex) {
System.err.println("connect对象建立错误!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这是什么意思啊,
链接数据库其实很简单具体代码如下:
package db;
import java.sql.*;
public class DB {
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
public DB(){}
public Connection getConnection(){
String url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true
链接数据库其实很简单具体代码如下:
package db;
import java.sql.*;
public class DB {
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
public DB(){}
public Connection getConnection(){
String url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的密码是不是输错了,它说用现在的密码访问被拒
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询