This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18202|Reply: 1

[Source] Java operation database DBHelper code

[Copy link]
Posted on 11/25/2014 1:09:23 PM | | |
  1. package online.school.util;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.Properties;

  7. public class DBHelper {
  8.         
  9.         private static String driver;
  10.         private static String url;
  11.         private static String username;
  12.         private static String password;
  13.         private static Connection conn;

  14.         private static void init(){
  15.                 try {
  16.                         Properties p = new Properties();
  17.                         p.load(DBHelper.class.getResourceAsStream("/jdbc.properties"));
  18.                         driver = p.getProperty("driver");
  19.                         url = p.getProperty("url");
  20.                         username = p.getProperty("username");
  21.                         password = p.getProperty("password");
  22.                 } catch (IOException e) {
  23.                         // TODO Auto-generated catch block
  24.                         e.printStackTrace();
  25.                 }
  26.         }
  27.         
  28.         public static Connection getConn(){
  29.                 init();
  30.                 try {
  31.                         Class.forName(driver);
  32.                         //获取一个连接对象connection
  33.                         conn = DriverManager.getConnection(url,username,password);
  34.                 } catch (ClassNotFoundException e) {
  35.                         // TODO Auto-generated catch block
  36.                         e.printStackTrace();
  37.                 } catch (SQLException e) {
  38.                         // TODO Auto-generated catch block
  39.                         e.printStackTrace();
  40.                 }
  41.                 return conn;
  42.         }
  43.         
  44.         public static void closeConn(){
  45.                 try {
  46.                         if (conn != null){
  47.                                 conn.close();
  48.                         }
  49.                 } catch (SQLException e) {
  50.                         // TODO Auto-generated catch block
  51.                         e.printStackTrace();
  52.                 }
  53.         }
  54. }
Copy code






Previous:CorelDRAW 12 Chinese Simplified Version Download
Next:Get a "dark cloud" invitation code today and become a white hat
Posted on 11/25/2014 7:30:20 PM |
learned
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com