sql server操作类(本人自己写的)

王朝学院·作者佚名  2016-05-26
窄屏简体版  字體: 小  |  中  |  大  |  超大  

sql server操作类(本人自己写的)package com.mytest;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PReparedStatement;import java.sql.ResultSet;import java.util.Vector;public class DBUtil { //定义连接数据库需要的 Connection ct=null; PreparedStatement pS=null; ResultSet rS=null; public ResultSet getSlect(String sql,Object ...params){ Vector rowDate=new Vector(); Vector columnDate =new Vector(); try { ct = connectWithDB(); pS=ct.prepareStatement(sql); for(int i = 0;i < params.length;i++){ pS.setObject(i+1, params[i]); } rS=pS.executeQuery(); } catch (Exception e) { // TODO: handle exception }finally{ return rS; } } /************修改数据库操作*********************/ public int update(String sql,Object ...params){ int executeUpdate_int = 0; try { ct = connectWithDB(); pS=ct.prepareStatement(sql); for(int i = 0;i < params.length;i++){ pS.setObject(i+1, params[i]); } //执行操作 executeUpdate_int = pS.executeUpdate(); } catch (Exception e) { // TODO: handle exception }finally{ shutDownDB(); return executeUpdate_int; } } /************连接数据库*********************/ private Connection connectWithDB(){ Connection connection = null; try { //加载驱动 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=spdb","sa","123456"); } catch (Exception e) { // TODO: handle exception } return connection; } /************关闭数据库的相关连接*********************/ public void shutDownDB(){ try { if(rS!=null) rS.close(); if(pS!=null) pS.close(); if(ct!=null) ct.close(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } } }

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航