package com.qh.qhutil.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
private static final String DBDRIVER="com.mysql.jdbc.Driver";//驱动类类名
private static final String DBNAME="com_eduwo";//数据库名
private static final String DBURL="jdbc:mysql://localhost:3306/"+DBNAME;//连接URL
private static final String DBUSER="root";//数据库用户名
private static final String DBPASSWORD="qianhao";//数据库密码
private static String DBurl ="jdbc:mysql://localhost:3306/?user=root&password=qianhao&useUnicode=true&characterEncoding=utf-8" ;
//myDB为数据库名
private static Connection Connect = null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
/**
* 获取数据库连接
* @author lcg
* @date 2014年9月24日11:48:49
* @return java.sql.Connection;
*/
public static Connection getConnection(){
// com.mysql.jdbc.Driver
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {//Connect=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//获得连接对象
Connect = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//获得连接对象
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Connect;
}
/**
* 获取创建数据库连接状态
* @author lcg
* @date 2014年9月24日11:48:49
* @return java.sql.Statement;
*/
public static Statement getStatement(){
Statement state = null;
try {
state = DBConnection.getConnection().createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return state;
}
/**
* 获取创建数据库连接状态
* @author lcg
* @date 2014年9月24日11:48:49
* @return java.sql.Statement;
*/
public static void closeConnection(){
if(Connect != null){
try {
Connect.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ResultSet select(String sql)throws Exception{
try{
Connect=getConnection();
ps=Connect.prepareStatement(sql);
rs=ps.executeQuery(sql);
return rs;
}catch(SQLException sqle){
throw new SQLException("select data Exception: "+sqle.getMessage());
}catch(Exception e){
throw new Exception("System error: "+e.getMessage());
}
}
/*//插入数据
publicintinsert(Stringsql)throwsException{
intnum=0;//计数
try{
Connect=getConnection();
ps=Connect.prepareStatement(sql);
num=ps.executeUpdate();
}catch(SQLExceptionsqle){
thrownewSQLException("insert data Exception: "+sqle.getMessage());
}finally{
try{
if(ps!=null){
ps.close();
}
}catch(Exceptione){
thrownewException("ps close exception: "+e.getMessage());
}
try{
if(Connect!=null){
Connect.close();
}
}catch(Exceptione){
thrownewException("Connect close exception: "+e.getMessage());
}
}
returnnum;
}*/
}
最近下载更多
王乐22222 LV10
2022年5月13日
醉何妨 LV9
2019年7月26日
yongzheng132 LV17
2019年6月25日
sky19961212 LV18
2018年12月26日
wsupsup LV16
2018年9月5日
haoyue_fw LV5
2017年11月15日
676323640 LV4
2017年8月29日
幕天席地 LV1
2017年8月24日
pengyang LV2
2017年3月11日
wyzl3321 LV25
2016年12月22日
最近浏览更多
WBelong LV8
2023年12月19日
微信网友_5992582549164032 LV6
2023年2月16日
yymmdm LV6
2022年8月10日
喃喵xxxx LV6
2022年4月20日
crosa_Don LV18
2022年3月31日
dukie123 LV1
2022年2月14日
开发哈哈
2022年1月17日
暂无贡献等级
charles1256 LV11
2021年12月15日
tangjj7260 LV18
2021年11月12日
byj1987 LV18
2021年7月8日

