-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBManager.java
123 lines (100 loc) · 2.8 KB
/
DBManager.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*package mnregapkg;
import java.sql.*;
public class DBManager {
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
private final String URL="jdbc:mysql://localhost:3306/mydb";
public Connection getConnection(){
//step1:
try{
Class.forName();
}catch(ClassNotFoundException e){
System.out.println("Error:1 Driver not found");
}
//step2:
try{
con= DriverManager.getConnection(URL, user, password);
}catch(SQLException e){
System.out.println("Error:2 Connection not established");
}
return con;
}
}
*/
package mnregapkg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Sumit
*/
public class DBManager {
//fields:
private Connection con=null;
private Statement st= null;
private ResultSet rs=null;
private int rows=0;
private final String URL="jdbc:mysql://localhost:3306/mydb";
private final String USER="root";
private final String PASSWORD="root";
//methods:
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
System.out.println("ERROR 1: Driver Error!");
}
try{
con=DriverManager.getConnection(URL, USER, PASSWORD);
}catch(SQLException ex){
System.out.println("ERROR 2: DateBase not connected");
}
return con;
}
public Statement getStatement(Connection conn){
try{
st=conn.createStatement();
}catch(SQLException ex){
ex.printStackTrace();
System.out.println("ERROR 3: Statement generation failure");
}
return st;
}
public boolean executeInsertQuery(Statement st,String query){
try{
rows=st.executeUpdate(query);
}catch(SQLException ex){
ex.printStackTrace();
System.out.println("ERROR 3: bbgbbgb Systemfailed");
}
if (rows>0)
return true;
else
return false;
}
public ResultSet executeSelectQuery(Statement st,String query){
try{
rs=st.executeQuery(query);
}catch(SQLException ex){
ex.printStackTrace();
System.out.println("Statement Failure");
}
return rs;
}
public boolean closeConnection(Connection conn){
try{
if(rs!=null)
rs.close();
if(conn!=null)
conn.close();
}catch(SQLException ex){
ex.printStackTrace();
System.out.println("Connection doesnot closed successfully");
}
return true;
}
}