package com.iok.jee.dbc;import com.mysql.jdbc.*;import java.sql.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Statement;import java.util.Properties;import ?java.sql.DriverManager;/*** Created by Administrator on 2018/1/20 0020.*/public class Dbutils {? ?private static Properties ? properties;? ?private static String ?driverClassName;? ?private static String url;? ?private static String userName;? ?private static String password;? ?static {? ? ? ?try {? ? ? ? ? ?properties = new Properties();? ? ? ? ? ?properties.load(Dbutils.class.getClassLoader().getResourceAsStream("jdbc.properties"));? ? ? ? ? ?driverClassName = properties.getProperty("driverClassName");? ? ? ? ? ?url = properties.getProperty("url");? ? ? ? ? ?userName = properties.getProperty("userName");? ? ? ? ? ?password = properties.getProperty("password");? ? ? ? ? ?Class.forName(driverClassName);/*加載驅(qū)動*/? ? ? ? /* ? DriverManager.registerDriver(new com.mysql.jdbc.Driver());*/? ? ? ?}catch (Exception e){? ? ? ? ? ?throw ?new ExceptionInInitializerError("類加載驅(qū)動失敗!");? ? ? ?}? ?}? ?/*連接驅(qū)動*/? ?public ?static Connection getConnection(){? ? ? ? ?Connection conn=null;? ? ? ?try{? ? ? ? conn = ? DriverManager.getConnection(url,userName,password);? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();;? ? ? ?}? ? ? ?return ?conn;? ?}? ?/*獲取語句對象*/? ?public ?static java.sql.Statement getStatment(java.sql.Connection ?conn){? ? ? ?java.sql.Statement stat=null;? ? ? ?if(conn==null){? ? ? ? ? ?throw ?new IllegalArgumentException("連接對象為空無法獲取語句對象");? ? ? ?}? ? ? ?try{? ? ? ? ? ?stat= conn.createStatement();? ? ? ?}catch (SQLException e){? ? ? ? ? ? ?e.printStackTrace();;? ? ? ?}? ? ? ? ? ? return ?stat;? ? }/*獲取預(yù)編譯語句對象*/? ?public ?static java.sql.PreparedStatement getPreparedStatement(java.sql.Connection ?conn,String sql){? ? ? ? PreparedStatement ?preparedStat=null;? ? ? ?if(conn==null){? ? ? ? ? ?throw ?new IllegalArgumentException("連接對象為空無法獲取預(yù)編譯語句對象");? ? ? ?}? ? ? ?if(sql==null||"".equals(sql)){? ? ? ? ? ?throw ?new IllegalArgumentException("sql語句為空無法獲取預(yù)編譯語句對象");? ? ? ?}? ? ? ?try{? ? ? ? ? ?preparedStat= conn.prepareStatement(sql);? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();;? ? ? ?}? ? ? ?return ?preparedStat;? ?}? ?public ?static ?void ?closeConnection ?(java.sql.Connection conn){? ? ? ? ? try{? ? ? ? ? ? ? if(conn!=null){/*如果連接不為空*/? ? ? ? ? ? ? ? ? if(!conn.isClosed()){/*如果連接未關(guān)閉*/? ? ? ? ? ? ? ? ? ? ? conn.close();/*關(guān)閉連接*/? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? }? ? ? ? ? }catch (SQLException e){? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? }? ? ? ? ? }? ?/*關(guān)閉語句對象*/? ?public ?static ?void ?closeStatement (java.sql.Statement stat){? ? ? ?try{? ? ? ? ? ?if(stat!=null){/*如果連接不為空*/? ? ? ? ? ? ? ?if(!stat.isClosed()){/*如果連接未關(guān)閉*/? ? ? ? ? ? ? ? ? ?stat.close();/*關(guān)閉連接*/? ? ? ? ? ? ? ?}? ? ? ? ? ?}? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();? ? ? ?}? ?}? ?/*關(guān)閉結(jié)果集*/? ?public ?static ?void ?closeResult (ResultSet rs){? ? ? ?try{? ? ? ? ? ?if(rs!=null){/*如果連接不為空*/? ? ? ? ? ? ? ?if(!rs.isClosed()){/*如果連接未關(guān)閉*/? ? ? ? ? ? ? ? ? ?rs.close();/*關(guān)閉連接*/? ? ? ? ? ? ? ?}? ? ? ? ? ?}? ? ? ?}catch (SQLException e){? ? ? ? ? ?e.printStackTrace();? ? ? ?}? ?}}添加回答2回答
添加回答
舉報
0/150
提交
取消