日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql dbutil_DBUtil连接数据库

發(fā)布時間:2023/12/9 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql dbutil_DBUtil连接数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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 命令對象//打開連接//關(guān)閉連接//得到一個連接對象//查詢(有參,無參)//修改(有參,無參)

static Connection conn = null;static Statement stmt = null;static PreparedStatement pstmt=null;//驅(qū)動,服務(wù)器地址,登錄用戶名,密碼

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() {//加載驅(qū)動

try{

Class.forName(DBDRIVER);

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

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}

}//關(guān)閉連接

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();

}

}//得到一個連接對象,當(dāng)用戶使用DBUtil無法解決個性問題時//可以通過本方法獲得連接對象

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

open();

}catch(SQLException e) {

e.printStackTrace();

}returnconn;

}//executeQuery//executeUpdate//execute//獲得查詢的數(shù)據(jù)集//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;

}//修改表格內(nèi)容

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;

}//如果執(zhí)行的查詢或存儲過程,會返回多個數(shù)據(jù)集,或多個執(zhí)行成功記錄數(shù)//可以調(diào)用本方法,返回的結(jié)果,//是一個List或List集合

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

open();//保證連接是成功的

stmt =conn.createStatement();

b=stmt.execute(sql);//true,執(zhí)行的是一個查詢語句,我們可以得到一個數(shù)據(jù)集//false,執(zhí)行的是一個修改語句,我們可以得到一個執(zhí)行成功的記錄數(shù)

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;//只是為了關(guān)閉命令對象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;//只是為了關(guān)閉命令對象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,執(zhí)行的是一個查詢語句,我們可以得到一個數(shù)據(jù)集//false,執(zhí)行的是一個修改語句,我們可以得到一個執(zhí)行成功的記錄數(shù)

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;

}//調(diào)用存儲過程 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;

}/** 調(diào)用存儲過程,并有輸出參數(shù)

* @procName ,存儲過程名稱:proc_Insert(?,?)

* @in ,輸入?yún)?shù)集合

* @output,輸出參數(shù)集合

* @type,輸出參數(shù)類型集合

**/

public staticObject executeOutputProcedure(String procName,

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

Object result= null;try{

CallableStatement cstmt= conn.prepareCall("{call "+procName+"}");//設(shè)置存儲過程的參數(shù)值

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();//獲取輸出參數(shù)的值

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;

}//時間轉(zhuǎn)換

public staticDate date(String date_str) {try{

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

Timestamp timestampnow = new Timestamp(cal.getTimeInMillis());//轉(zhuǎn)換成正常的日期格式

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;

}

};

}

總結(jié)

以上是生活随笔為你收集整理的mysql dbutil_DBUtil连接数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。