Made database connection class for my project that using mysql jdbc. I'm putting the code here incase anyone may find it usefull. Or you can download it from http://vc-digital.com/ConnectionDB.java
/*
* Created on March 25, 2005
*
*/
package sim;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
import java.lang.String;
/**
* This source code is license under GPL
* Still working on password encryption
* You may use it, hack it and do what ever you want but please put my name as a credit if you
* are using this code for your application...
* If you find this code usefull for you please donate to the poor and homeless in your
* hood... giving to others will make you a better person. Trust me...
* Author o3ng. Contact me for any suggestion or question at avenpace@gmail.com
*
*/
class ConnectionDB {
private String [] cfg, cfgx;
private String read;
private Connection connection, connect;
private ResultSet rs;
public ConnectionDB() {
driverReg();
}
private String readConf() {
File fl = new File("db.conf");
try {
BufferedReader in = new BufferedReader(new FileReader(fl));
//String read;
read = in.readLine();
} catch (FileNotFoundException ee){
System.err.println("Can't find config file db.conf");
} catch (IOException oi){
}
return (read);
}
public ResultSet xecuteQuery(String query) {
String conRead = readConf();
System.out.println(conRead);
String delim = ":";
Scanner cfgFullx = new Scanner(conRead);
cfgFullx.useDelimiter(delim);
cfgx = new String[4];
int i = 0;
while (cfgFullx.hasNext()){
cfgx[i] = cfgFullx.next();
i++;
}
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://" + cfgx[0] +"/" + cfgx[1] + "?user=" + cfgx[2] + "&password=" + cfgx[3]);
Statement statemen = connection.createStatement();
ResultSet rst = statemen.executeQuery(query);
setResult(rst);
//statemen.close();
//connection.close();
} catch (SQLException e) {
System.out.println("Connection error to the database");
}
getResult();
return this.rs;
}
public void updateQuery(String query) {
String conRead = readConf();
System.out.println(conRead);
String delim = ":";
Scanner cfgFull = new Scanner(conRead);
cfgFull.useDelimiter(delim);
cfg = new String[4];
int i = 0;
while (cfgFull.hasNext()){
cfg[i] = cfgFull.next();
//System.out.println(cfg[i]);
i++;
}
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://" + cfg[0] +"/" + cfg[1] + "?user=" + cfg[2] + "&password=" + cfg[3]);
System.out.println(query);
Statement statement = connect.createStatement();
statement.executeUpdate(query);
statement.close();
connect.close();
} catch (SQLException e) {
System.out.println("Connection error to the database");
}
}
public void setResult(ResultSet frs){
this.rs = frs;
}
public ResultSet getResult(){
return this.rs;
}
public void driverReg(){
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("Register driver");
} catch (Exception e){
System.err.println("Can't find jdbc driver");
System.exit(1);
}
}
}
The db hostname, db name, user and password store in external file called db.conf
The content of the db.conf file is "dbhostname:dbname:dbuser:dbpassword"
You can use this class by construct an ConnectionDB class object, for example
ConnectionDB conn = new ConnectionDB();
queryUp = "INSERT INTO simtable SET sesi='test'";
conn.updateQuery(queryUp);
/*
* Created on March 25, 2005
*
*/
package sim;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
import java.lang.String;
/**
* This source code is license under GPL
* Still working on password encryption
* You may use it, hack it and do what ever you want but please put my name as a credit if you
* are using this code for your application...
* If you find this code usefull for you please donate to the poor and homeless in your
* hood... giving to others will make you a better person. Trust me...
* Author o3ng. Contact me for any suggestion or question at avenpace@gmail.com
*
*/
class ConnectionDB {
private String [] cfg, cfgx;
private String read;
private Connection connection, connect;
private ResultSet rs;
public ConnectionDB() {
driverReg();
}
private String readConf() {
File fl = new File("db.conf");
try {
BufferedReader in = new BufferedReader(new FileReader(fl));
//String read;
read = in.readLine();
} catch (FileNotFoundException ee){
System.err.println("Can't find config file db.conf");
} catch (IOException oi){
}
return (read);
}
public ResultSet xecuteQuery(String query) {
String conRead = readConf();
System.out.println(conRead);
String delim = ":";
Scanner cfgFullx = new Scanner(conRead);
cfgFullx.useDelimiter(delim);
cfgx = new String[4];
int i = 0;
while (cfgFullx.hasNext()){
cfgx[i] = cfgFullx.next();
i++;
}
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://" + cfgx[0] +"/" + cfgx[1] + "?user=" + cfgx[2] + "&password=" + cfgx[3]);
Statement statemen = connection.createStatement();
ResultSet rst = statemen.executeQuery(query);
setResult(rst);
//statemen.close();
//connection.close();
} catch (SQLException e) {
System.out.println("Connection error to the database");
}
getResult();
return this.rs;
}
public void updateQuery(String query) {
String conRead = readConf();
System.out.println(conRead);
String delim = ":";
Scanner cfgFull = new Scanner(conRead);
cfgFull.useDelimiter(delim);
cfg = new String[4];
int i = 0;
while (cfgFull.hasNext()){
cfg[i] = cfgFull.next();
//System.out.println(cfg[i]);
i++;
}
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://" + cfg[0] +"/" + cfg[1] + "?user=" + cfg[2] + "&password=" + cfg[3]);
System.out.println(query);
Statement statement = connect.createStatement();
statement.executeUpdate(query);
statement.close();
connect.close();
} catch (SQLException e) {
System.out.println("Connection error to the database");
}
}
public void setResult(ResultSet frs){
this.rs = frs;
}
public ResultSet getResult(){
return this.rs;
}
public void driverReg(){
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("Register driver");
} catch (Exception e){
System.err.println("Can't find jdbc driver");
System.exit(1);
}
}
}
The db hostname, db name, user and password store in external file called db.conf
The content of the db.conf file is "dbhostname:dbname:dbuser:dbpassword"
You can use this class by construct an ConnectionDB class object, for example
ConnectionDB conn = new ConnectionDB();
queryUp = "INSERT INTO simtable SET sesi='test'";
conn.updateQuery(queryUp);
Comments