package com.sdms.main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* @Author: Ron June G. Lopez
* @Email: ronjune16@gmail.com
*
* */
public class DatabaseManager {
private Connection conn;
private Statement stmt;
private ResultSet rs;
private boolean isDriver = false;
private String url;
private String username;
private String password;
public DatabaseManager(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
public void setUrl(String host, int port, String dbName) {
// Example url : "jdbc:mysql://localhost:3306/test"
this.url = (host + ":" + port).concat("/" + dbName);
}
public String getUrl() {
return this.url;
}
public void dbOpen() {
try {
if (this.conn == null) {
System.out.println("[Connecting]..");
Class.forName("com.mysql.jdbc.Driver");
this.isDriver = true;
this.conn = DriverManager.getConnection(this.url,
this.username, this.password);
System.out.println("[Connected]");
} else {
System.out.println("*Cannot duplicate connection.");
}
} catch (SQLException e) {
System.out
.println( e.getMessage());
} catch (ClassNotFoundException e) {
System.out
.println( e.getMessage());
} finally {
if (!this.isDriver) {
System.out.println("[Connection failed] ");
}
}
}
public void dbClose() {
try {
if (this.conn != null) {
/* Disconnecting */
System.out.println("[Disconnecting]..");
this.conn.close();
this.conn = null;
/* Disconnected */
System.out.println("[Disconnected] ");
} else {
/* */
System.out.println("[You are not connected]");
}
} catch (SQLException e) {
//
System.out.println( e.getMessage());
} catch (Exception e) {
//
System.out.println( e.getMessage());
}
}
public void dbSelectStatement(String query) {
if (query != null&& this.conn!=null) {
try {
this.stmt = conn.createStatement();
this.rs = this.stmt.executeQuery(query);
while (this.rs.next()) {
System.out.println(this.rs.getObject(3).toString());
}
this.rs.close();
this.rs = null;
this.stmt.close();
this.stmt = null;
} catch (SQLException e) {
System.out.println("sqlExc." + e.getMessage());
} catch (Exception e) {
//
System.out.println("exc." + e.getMessage());
}
}
}
public void dbInsertStatement(String query) {
if (query != null&& this.conn!=null) {
try {
this.stmt = conn.createStatement();
this.stmt.executeUpdate(query);
System.out.println("Inserted records into the table.");
this.stmt.close();
this.stmt = null;
} catch (SQLException e) {
System.out.println("sqlExc." + e.getMessage() + "");
} catch (Exception e) {
//
System.out.println("exc." + e.getMessage());
}
}
}
public void dbLoginQuery(String query, String password) {
// sample query SELECT <field name=password> FROM <table name=user>
// WHERE <field name=username> ='@Param'
if (query != null && this.conn!=null) {
try {
this.stmt = conn.createStatement();
this.rs = this.stmt.executeQuery(query);
if (this.rs.next()) {
String dbPassword = this.rs.getString(1);
if (dbPassword.equals(password)) {
// login successful
System.out.println("login successful");
} else {
// login unsuccessful
System.out.println("login unsuccessful");
}
} else {
// user name not found
System.out.println("username not found");
}
this.rs.close();
this.rs = null;
this.stmt.close();
this.stmt = null;
} catch (SQLException e) {
//
System.out.println("sqlex." + e.getMessage());
} catch (Exception e) {
//
System.out.println("ex." + e.getMessage());
}
}
}
public static void main(String[] args) {
DatabaseManager dm = new DatabaseManager("", "root", "password");
dm.setUrl("jdbc:mysql://localhost", 3306, "");
dm.dbOpen();
// dm.dbInsertStatement("INSERT INTO `dbers`.`tbluserinfo` (`UserID`, `Fname`, `Mname`, `Lname`, `Sex`, `DeptID`, `CourseID`, `YearLvl`, `UserType`, `Deleted`) VALUES ('4', 'Ron June', 'Garcia', 'Lopez', '2', '1', '1', '3', '3', '0')");
// dm.dbSelectStatement("SELECT * FROM dbers.tbluserinfo");
// dm.dbRecordExist("SELECT DISTINCT UserID FROM tbluserinfo WHERE EXISTS(SELECT UserID FROM tbluserinfo WHERE UserID = 5)");
dm.dbLoginQuery(
"SELECT password FROM test.tbluser WHERE username = 'test'",
"tesst");
dm.dbClose();
dm.dbOpen();
dm.dbSelectStatement("SELECT * FROM dbers.tbluserinfo");
dm.dbClose();
dm.dbClose();
dm.dbClose();
dm.dbOpen();
dm.dbOpen();
dm.dbOpen();
dm.dbSelectStatement("SELECT * FROM dbers.tbluserinfo");
dm.dbOpen();
dm.dbClose();
dm.dbClose();
dm.dbOpen();
dm.dbOpen();
dm.dbOpen();
dm.dbOpen();
}
}
No comments:
Post a Comment