×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dauda Ainoo
Added: Apr 26, 2020 9:31 AM
Views: 4323
Tags: no tags
  1.  
  2. Submit another Data table
  3. UserAccountsDAO.java UserAccounts.java DBConn.java DBException.java
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. /**
  12.  * @author srinivas
  13.  * This file is to create Connections and Closing connections and statements.
  14.  */
  15. public class DBConn {
  16.         Connection con = null;
  17.         static String className = "class sodhana.sdb.connection.DBConnection";
  18.  
  19.         /**
  20.          * @return
  21.          * @throws ClassNotFoundException
  22.          * @throws SQLException
  23.          * To Create Connections
  24.          */
  25.         public static Connection getConnection() throws ClassNotFoundException, SQLException {
  26.                 Class.forName("com.mysql.jdbc.Driver");
  27.                 Connection connection = null;
  28.                 connection = DriverManager.getConnection(
  29.                                 "jdbc:mysql://localhost:3306/world", "root", "srinivas");
  30.                 return connection;
  31.         }
  32.  
  33.         /**
  34.          * @param con
  35.          * @param stmt
  36.          * @param rs
  37.          * To close statements, result sets and Connection
  38.          */
  39.         public static void close(Connection con, Statement stmt, ResultSet rs) {
  40.                 try {
  41.                         if (rs != null) {
  42.                                 rs.close();
  43.                         }
  44.                         if (stmt != null) {
  45.                                 stmt.close();
  46.                         }
  47.                         if (con != null) {
  48.                                 con.close();
  49.                         }
  50.                 } catch (SQLException e) {
  51.                         e.printStackTrace();
  52.                 } catch (Exception e) {
  53.                         e.printStackTrace();
  54.                 }
  55.         }
  56.  
  57.         /**
  58.          * @param con
  59.          * @param stmt
  60.          * To close statements and Connection
  61.          */
  62.         public static void close(Connection con, Statement stmt) {
  63.                 try {
  64.                         if (stmt != null) {
  65.                                 stmt.close();
  66.                         }
  67.                         if (con != null) {
  68.                                 con.close();
  69.                         }
  70.                 } catch (SQLException e) {
  71.                         e.printStackTrace();
  72.                 } catch (Exception e) {
  73.                         e.printStackTrace();
  74.                 }
  75.         }
  76.  
  77.         /**
  78.          * @param con
  79.          * @param pstmt
  80.          * @param rs
  81.          * To Close PreparedStatement and Connection
  82.          */
  83.         public static void close(Connection con, PreparedStatement pstmt,
  84.                         ResultSet rs) {
  85.                 try {
  86.                         if (rs != null) {
  87.                                 rs.close();
  88.                         }
  89.                         if (pstmt != null) {
  90.                                 pstmt.close();
  91.                         }
  92.                         if (con != null) {
  93.                                 con.close();
  94.                         }
  95.                 } catch (SQLException e) {
  96.                         e.printStackTrace();
  97.                 } catch (Exception e) {
  98.                         e.printStackTrace();
  99.                 }
  100.         }
  101.  
  102.         /**
  103.          * @param con
  104.          * To close connection
  105.          */
  106.         public static void closeConnection(Connection con) {
  107.                 try {
  108.                         if (con != null)
  109.                                 con.close();
  110.                 } catch (SQLException e) {
  111.                         e.printStackTrace();
  112.                 } catch (Exception e) {
  113.                         e.printStackTrace();
  114.                 }
  115.         }
  116.  
  117.         /**
  118.          * @param conn
  119.          * @throws SQLException
  120.          * To Commit and Close the connection
  121.          */
  122.         public static void commitAndClose(Connection conn) throws SQLException {
  123.                 conn.commit();
  124.                 conn.close();
  125.         }
  126.  
  127. }                      
  128.