JAVA jdbc擷取資料庫連接

來源:互聯網
上載者:User

標籤:

JDBC擷取資料庫連接的協助類

 1 import java.io.InputStream; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.ResultSet; 5 import java.sql.Statement; 6 import java.util.Properties; 7  8 public class jdbcUtil { 9 10 private static String driver;11 private static String url;12 private static String id;13 private static String password;14 15 //負載檔案,擷取配置參數16 static{17 Properties prop=new Properties();18 InputStream is;19 try {20 is = jdbcUtil.class.getClassLoader()21 .getResourceAsStream("com/sy/db/db.properties");22 prop.load(is);23 driver = prop.getProperty("driver");24 url = prop.getProperty("url");25 id = prop.getProperty("id");26 password = prop.getProperty("password");27 } catch (Exception e1) {28 e1.printStackTrace();29 }30 }31 32 //註冊驅動33 static{34 try {35 Class.forName(driver);36 } catch (Exception e) {37 e.printStackTrace();38 }39 }40 41 //取得串連42 public static Connection getMysqlConnection(){43 Connection conn=null;44 try {45 conn=DriverManager.getConnection(url,id,password);46 } catch (Exception e) {47 e.printStackTrace();48 }49 return conn;50 }51 52 //關閉流53 public static void close(ResultSet rs){54 if(null!=rs){55 try {56 rs.close();57 } catch (Exception e) {58 e.printStackTrace();59 }60 }61 }62 public static void close(Statement state){63 if(null!=state){64 try {65 state.close();66 } catch (Exception e) {67 e.printStackTrace();68 }69 }70 }71 public static void close(Connection conn){72 if(null!=conn){73 try {74 conn.close();75 } catch (Exception e) {76 e.printStackTrace();77 }78 }79 }80 }
View Code

 

JAVA jdbc擷取資料庫連接

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.