100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > mysql dbutil_DBUtil连接数据库

mysql dbutil_DBUtil连接数据库

时间:2019-02-04 08:25:05

相关推荐

mysql dbutil_DBUtil连接数据库

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;

}

};

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。