jdbc连接mysql数据库失败的原因

2024-11-07 20:37:30
推荐回答(2个)
回答(1):

package com.extranauts.shixun2.m.db.tools;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtils {
private static Properties p = new Properties();
private static DBUtils d = new DBUtils();
static{
try {
p.load(d.getClass().getResourceAsStream("init.properties"));
} catch (IOException e) {
System.out.println("配置文件不存在" + e.getMessage());
}
}
public static Connection getConnection() {
Connection con = null;
// 加载配置文件

// 提取配置文件中的项
// 读取配置文件中的数据库连接项
String ip = p.getProperty("ip").trim();
String dbtype = p.getProperty("dbtype").trim();
String dbname = p.getProperty("dbname").trim();
String port = p.getProperty("port").trim();
String username = p.getProperty("username").trim();
String password = p.getProperty("password").trim();
// 判断链接数据库的种类
if (dbtype.equalsIgnoreCase("mysql")) {
// 创建链接
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("mysql驱动未找到" + e.getMessage());
}
StringBuffer s = new StringBuffer();
s.append("jdbc:mysql://");
s.append(ip);
s.append(":");
s.append(port);
s.append("/");
s.append(dbname);
s.append("?user=");
s.append(username);
s.append("&password=");
s.append(password);
s.append("&useUnicode=true&characterEncoding=gb2312");
// myDB为数据库名
try {
con= DriverManager.getConnection(s.toString());
} catch (SQLException e) {
System.out.println("数据库连接项不正确" + e.getMessage());
}
}
if (dbtype.equalsIgnoreCase("sqlserver")) {
// 创建链接
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
System.out.println("sqlserver驱动未找到" + e.getMessage());
}
StringBuffer s = new StringBuffer();
s.append("jdbc:microsoft:sqlserver://");
s.append(ip);
s.append(":");
s.append(port);
s.append(";DatabaseName=");
s.append(dbname);

// mydb为数据库
try {
con= DriverManager.getConnection(s.toString(),username,password);
} catch (SQLException e) {
System.out.println("数据库连接项不正确" + e.getMessage());
}
}
// if (dbtype.equals("oracle")) {
//// 创建链接
// try {
// Class.forName("oracle.jdbc.driver.OracleDriver");
// } catch (ClassNotFoundException e) {
// System.out.println("oracle数据库驱动未找到" + e.getMessage());
// }
// String url="jdbc:oracle:thin:@"+ip+":"+port+":"+dbname;
//// orcl为数据库的SID
// try {
// con= DriverManager.getConnection(url,username,password);
// } catch (SQLException e) {
// System.out.println("数据库连接项不正确" + e.getMessage());
// }
// }

return con;
}

public static void close(Connection con,PreparedStatement pstam , ResultSet rs){
if(con != null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstam != null){
try {
pstam.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

没顾上看你的 这是我以前用过的

回答(2):

public
static Connection getConnection(){

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Connection conn=null;

try {

conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/user?useEncode=true&characterEncoding=utf-8","root","root");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

有没有报错?