DBUtil.java
上传用户:zhc3n3
上传日期:2022-07-30
资源大小:2750k
文件大小:2k
- /**
- *
- */
- package com.t11.dao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- /**
- * @author student
- *
- */
- public class DBUtil {
- Connection conn = null;
- public Connection getConnection(){
-
-
- try{
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- String url = "jdbc:sqlserver://localhost:1433;databaseName=addressBookDB";
- conn = DriverManager.getConnection(url,"sa","");
- }catch(Exception e){
- e.printStackTrace();
- }
- return conn;
- }
-
-
- public boolean excuUpdate(String sql){
- boolean bool = false;
- conn = this.getConnection();
- PreparedStatement pst = null;
- try{
- pst = conn.prepareStatement(sql);
- int count =pst.executeUpdate();
- bool = count>0;
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try{
- if(pst!=null){
- pst.close();
- }
- if(conn != null){
- conn.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return bool;
- }
-
- public ResultSet excuqury(String sql){
- ResultSet rs = null;
- conn = this.getConnection();
- PreparedStatement pst = null;
- try{
- pst = conn.prepareStatement(sql);
- rs = pst.executeQuery();
- }catch(Exception e){
- e.printStackTrace();
- }
- return rs;
- }
-
- public void colse(ResultSet rs){
- try{
- if(rs !=null){
- rs.close();
- }
- if(conn != null){
- conn.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }