-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDbConnection.java
51 lines (42 loc) · 1.37 KB
/
DbConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.leapfrog.academy.dbutil;
import com.leapfrog.academy.constant.DbConstant;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author admin
*/
public class DbConnection {
Connection connection=null;
PreparedStatement stmt=null;
public DbConnection(){
}
public void open() throws SQLException,ClassNotFoundException{
Class.forName(DbConstant.DB_DRIVER);
connection=DriverManager.getConnection(DbConstant.DB_URL,DbConstant.DB_USER,DbConstant.DB_PASSWORD);
}
public PreparedStatement init(String sql) throws SQLException{
stmt=connection.prepareStatement(sql);
return stmt;
}
public int executeUpdate() throws SQLException{
return stmt.executeUpdate();
}
public ResultSet executeQuery() throws SQLException{
return stmt.executeQuery();
}
public void close() throws SQLException{
if(connection!=null && !connection.isClosed()){
connection.close();
connection=null;
}
}
}