-
Notifications
You must be signed in to change notification settings - Fork 0
/
0ConnectionFactory.java
62 lines (53 loc) · 1.7 KB
/
0ConnectionFactory.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
/*
* 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 cdvirtualserver;
import java.sql.*;
import java.util.Vector;
/**
*
* @author Chayan-Dhaddha
*/
public class ConnectionFactory {
Connection con;
Statement stmt;
private ConnectionFactory(){
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/virtualclass_db","root","");
stmt=con.createStatement();
}catch(Exception ex){
System.out.println("Exception in db create...");
}
}
private static ConnectionFactory conn;
static {
conn=new ConnectionFactory();
}
public static ConnectionFactory getInstance(){
return conn;
}
public int setData(String query)throws Exception{
int n=stmt.executeUpdate(query);
return n;
}
public ResultSet getResultSet(String query)throws Exception{
ResultSet rs=stmt.executeQuery(query);
return rs;
}
public Vector<Vector> getData(String query)throws Exception{
ResultSet rs=stmt.executeQuery(query);
ResultSetMetaData rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
Vector<Vector> main=new Vector<Vector>();
while(rs.next()){
Vector<String> sub=new Vector<String>();
for(int i=1;i<=col;i++)
sub.add(rs.getString(i));
main.add(sub);
}
return main;
}
}