mysql dbdriver dburl_DBUtil连接数据库

该博客介绍了如何在Java中使用DBUtil工具类连接MySQL数据库。包括加载数据库驱动、配置DBDRIVER、DBURL、DBUID和DBPWD,以及执行查询和更新操作的方法。通过getConnection()获取连接,使用executeQuery()执行查询,使用executeUpdate()执行更新,如修改表格内容。
摘要由CSDN通过智能技术生成

packagecom.dz.product.dao;importjava.io.IOException;importjava.io.InputStream;import java.sql.*;importjava.text.ParsePosition;importjava.text.SimpleDateFormat;import java.util.*;importjava.util.Date;public classDBUtil {//连接对象//Statement 命令对象//打开连接//关闭连接//得到一个连接对象//查询(有参,无参)//修改(有参,无参)

static Connection conn = null;static Statement stmt = null;static PreparedStatement pstmt=null;//驱动,服务器地址,登录用户名,密码

staticString DBDRIVER;staticString DBURL;staticString DBUID;staticString DBPWD;static{ DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

DBURL = "jdbc:sqlserver://localhost:1433;databasename=YerGoMallPro";DBUID= "sa";//DBPWD = "@zdm168168...";

DBPWD = "123456";//打开连接

}

public static voidopen() {//加载驱动

try{

Class.forName(DBDRIVER);

conn=DriverManager.getConnection(DBURL,DBUID,DBPWD);

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}

}//关闭连接

public static voidclose() {try{if(stmt!=null)

stmt.close();if(conn!=null && !conn.isClosed())

conn.close();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}//得到一个连接对象,当用户使用DBUtil无法解决个性问题时//可以通过本方法获得连接对象

public staticConnection getConnection() {try{if(conn==null ||conn.isClosed())

open();

}catch(SQLException e) {

e.printStackTrace();

}returnconn;

}//executeQuery//executeUpdate//execute//获得查询的数据集//select * from student where name='' and sex=''

public staticResultSet executeQuery(String sql) {try{

open();//保证连接是成功的

stmt =conn.createStatement();returnstmt.executeQuery(sql);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}return null;

}//修改表格内容

public static intexecuteUpdate(String sql) {int result = 0;try{

open();//保证连接是成功的

stmt =conn.createStatement();

result=stmt.executeUpdate(sql);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{

close();

}returnresult;

}//如果执行的查询或存储过程,会返回多个数据集,或多个执行成功记录数//可以调用本方法,返回的结果,//是一个List或List集合

public staticObject execute(String sql) {boolean b=false;try{

open();//保证连接是成功的

stmt =conn.createStatement();

b=stmt.execute(sql);//true,执行的是一个查询语句,我们可以得到一个数据集//false,执行的是一个修改语句,我们可以得到一个执行成功的记录数

if(b){returnstmt.getResultSet();

}else{returnstmt.getUpdateCount();

}

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{if(!b) {

close();

}

}return null;

}//

//select * from student where name=? and sex=?

public staticResultSet executeQuery(String sql,Object[] in) {try{

open();//保证连接是成功的

PreparedStatement pst =conn.prepareStatement(sql);for(int i=0;i

pst.setObject(i+1, in[i]);

stmt= pst;//只是为了关闭命令对象pst

returnpst.executeQuery();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}return null;

}public static intexecuteUpdate(String sql,Object[] in) {try{

open();//保证连接是成功的

PreparedStatement pst =conn.prepareStatement(sql);for(int i=0;i

pst.setObject(i+1, in[i]);

stmt= pst;//只是为了关闭命令对象pst

returnpst.executeUpdate();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{

close();

}return 0;

}public staticPreparedStatement pstmt(String sql){

open();try{return pstmt =conn.prepareStatement(sql);

}catch(SQLException e) {//TODO 自动生成的 catch 块

e.printStackTrace();

}return null;

}public staticObject execute(String sql,Object[] in) {boolean b=false;try{

open();//保证连接是成功的

PreparedStatement pst =conn.prepareStatement(sql);for(int i=0;i

pst.setObject(i+1, in[i]);

b=pst.execute();//true,执行的是一个查询语句,我们可以得到一个数据集//false,执行的是一个修改语句,我们可以得到一个执行成功的记录数

if(b){

System.out.println("----");/*List list = new ArrayList();

list.add(pst.getResultSet());

while(pst.getMoreResults()) {

list.add(pst.getResultSet());

}*/

returnpst.getResultSet();

}else{

System.out.println("****");

List list = new ArrayList();

list.add(pst.getUpdateCount());while(pst.getMoreResults()) {

list.add(pst.getUpdateCount());

}returnlist;

}

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{if(!b) {

System.out.println("====");

close();

}

}return null;

}//调用存储过程 proc_Insert(?,?,?)

public staticObject executeProcedure(String procName,Object[] in) {

open();try{

procName= "{call "+procName+"(";

String link="";for(int i=0;i

procName+=link+"?";

link=",";

}

procName+=")}";

CallableStatement cstmt=conn.prepareCall(procName);for(int i=0;i

cstmt.setObject(i+1, in[i]);

}if(cstmt.execute())

{returncstmt.getResultSet();

}else{returncstmt.getUpdateCount();

}

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}return null;

}/** 调用存储过程,并有输出参数

* @procName ,存储过程名称:proc_Insert(?,?)

* @in ,输入参数集合

* @output,输出参数集合

* @type,输出参数类型集合

**/

public staticObject executeOutputProcedure(String procName,

Object[] in,Object[] output,int[] type){

Object result= null;try{

CallableStatement cstmt= conn.prepareCall("{call "+procName+"}");//设置存储过程的参数值

int i=0;for(;i

cstmt.setObject(i+1, in[i]);//print(i+1);

}int len = output.length+i;for(;i

cstmt.registerOutParameter(i+1,type[i-in.length]);//print(i+1);

}boolean b =cstmt.execute();//获取输出参数的值

for(i=in.length;i

output[i-in.length] = cstmt.getObject(i+1);if(b) {

result=cstmt.getResultSet();

}else{

result=cstmt.getUpdateCount();

}

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnresult;

}//时间转换

public staticDate date(String date_str) {try{

Calendar cal= Calendar.getInstance();//日期类

Timestamp timestampnow = new Timestamp(cal.getTimeInMillis());//转换成正常的日期格式

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改为需要的东西

ParsePosition pos = new ParsePosition(0);

java.util.Date current=formatter.parse(date_str, pos);

timestampnow= newTimestamp(current.getTime());returntimestampnow;

}catch(NullPointerException e) {return null;

}

};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值