我在eclipse中写了个简单的java连接mysql数据库的程序,总连不上,求指导!

2024-11-24 02:20:52
推荐回答(5个)
回答1:

这个问题,我有经验。程序没有错的情况下。
先停止mysql服务,然后打开命令行。输入mysqld_safe --skip-grant-tables
然后输入 mysql -u root
然后修改密码update user set Password = PASSWORD('新密码') where User ='root';
当然,密码也可以不改
然后flus privileges接着quit退出
然后重启mysql。
搞定

回答2:

我对这个也不是很熟悉,我从网上找的一个,我用了连接成功!你可以参考下。
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;
}

回答3:

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对象建立错误!");
}
}

回答4:

你这是什么意思啊,
链接数据库其实很简单具体代码如下:
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

回答5:

你的密码是不是输错了,它说用现在的密码访问被拒