java怎么连接mysql啊,标准的连接代码
2个回答
展开全部
package edu.fzu.hotelsys.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Administrator
*
*/
public class DBUtilsOld {
//数据库连接信息
private static final String CONN_URL="jdbc:mysql://localhost:3306/hotel_db";
private static final String USERNAME="hotelmgr";
private static final String PASSWD="abc123";
//单例实现
private static final DBUtilsOld ME=new DBUtilsOld();
public static DBUtilsOld getInstance(){
return ME;
}
private DBUtilsOld(){
}
/**
* 获得连接
* @return
*/
public Connection getConn(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(CONN_URL, USERNAME, PASSWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 释放资源
* @param conn
* @param pstmt
* @param rset
*/
public void releaseRes(Connection conn, PreparedStatement pstmt, ResultSet rset){
try {
if(rset!=null) rset.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
追问
驱动jar包怎么弄呢,大神
展开全部
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static String IP = "127.0.0.1";
private static String PORT = "3306";
private static final String USER_NAME = "root";
private static final String PASSWORD = "root";
private static final String DATABASE_NAME = "test_2015";
private static final String URL = new StringBuffer().append("jdbc:mysql://").append(IP).append(":").append(PORT).append("/").append(DATABASE_NAME).append("?useUnicode=true&characterEncoding=UTF-8").toString();
// private static final String URL = "jdbc:mysql://127.0.0.1:3306/test_2015?useUnicode=true&characterEncoding=UTF-8";
private static final ThreadLocal<Connection> t = new ThreadLocal<Connection>();
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = t.get();
if (conn == null) {
conn = createConnection();
t.set(conn);
}
return conn;
}
public static void close() {
Connection conn = t.get();
if (conn != null) {
try {
conn.close();
t.set(null);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static Connection createConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) throws SQLException {
Connection conn = DBUtil.getConnection();
String sql = "SELECT user_name AS userName, password FROM t_user WHERE user_id = ?";
PreparedStatement p = conn.prepareStatement(sql);
int userId = 1;
p.setObject(1, userId);
ResultSet r = p.executeQuery();
String userName = "";
String password = "";
if (r.next()) {
userName = r.getString("userName");
password = r.getString("password");
}
DBUtil.close();
System.out.println("用户名:" + userName);
System.out.println("密码:" + password);
}
}
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static String IP = "127.0.0.1";
private static String PORT = "3306";
private static final String USER_NAME = "root";
private static final String PASSWORD = "root";
private static final String DATABASE_NAME = "test_2015";
private static final String URL = new StringBuffer().append("jdbc:mysql://").append(IP).append(":").append(PORT).append("/").append(DATABASE_NAME).append("?useUnicode=true&characterEncoding=UTF-8").toString();
// private static final String URL = "jdbc:mysql://127.0.0.1:3306/test_2015?useUnicode=true&characterEncoding=UTF-8";
private static final ThreadLocal<Connection> t = new ThreadLocal<Connection>();
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = t.get();
if (conn == null) {
conn = createConnection();
t.set(conn);
}
return conn;
}
public static void close() {
Connection conn = t.get();
if (conn != null) {
try {
conn.close();
t.set(null);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static Connection createConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) throws SQLException {
Connection conn = DBUtil.getConnection();
String sql = "SELECT user_name AS userName, password FROM t_user WHERE user_id = ?";
PreparedStatement p = conn.prepareStatement(sql);
int userId = 1;
p.setObject(1, userId);
ResultSet r = p.executeQuery();
String userName = "";
String password = "";
if (r.next()) {
userName = r.getString("userName");
password = r.getString("password");
}
DBUtil.close();
System.out.println("用户名:" + userName);
System.out.println("密码:" + password);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询