📄 dboperations.java
字号:
/*
* Author :Prakash S
* Modified :
*
*/
//Package Declaration
package com;
//Import Statemenets
import java.sql.Connection; //Connection Class
import java.sql.DriverManager; //Driver Class
import java.sql.ResultSet; //Resultset Class
import java.sql.SQLException; //Exception Class
import java.sql.Statement; //Statement Class
public class DBOperations implements AppConstants
{
/*Declaration Section
* ------------------
* Connection: Hodls the Current Connection
* driver: Holds the Current Driver Used
* username: User Name to Connect to the Database
* password: User Password to Connect to the Database
* resultset: It holds the Currently Exectuted Query Results
* Source: Contains the Current Database URL
* Statement: Prepares the Current Statement to Execute
*
* */
private Connection connection = null;
private String driver = "";
private String password = "";
private ResultSet resultset = null;
private String source = "";
private Statement statemet = null;
private String username = "";
/*Default Constructor
* -----------------
* It initialises the Driver,URL,Database to be Used for all Operations
*
* */
public DBOperations()
{
setDbParameters();
}
/*Deletes a Record from the Database
* Accepts :a Query String returns
* Returns :No. of Rows Affected
* */
public int deleteFromTable(String deleteQuery)
{
int count = 0;
try
{
System.out.println("Quey:" + deleteQuery);
//Delete from the Table
statemet = connection.createStatement();
//Return Count of Records Affected
count = statemet.executeUpdate(deleteQuery);
} catch (SQLException e)
{
System.out.println(e.getLocalizedMessage());
return count;
}
return count;
}
/*Closes the Current Conection with the Database
* Accepts :Nil
* Returns :Nil
* */
public void disConnect()
{
try
{
if (resultset != null)
{
resultset.close();//Close the Current ResultSet
}
if (statemet != null)
{
statemet.close();//Close the Statement
}
if (connection != null)
{
connection.close();//Remove Current Database Connection (Disconnect)
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
System.out.println(e.getLocalizedMessage());
}
}
/*Deletes a Record from the Database
* Accepts :Nil
* Returns :A connnection Object THat Could be used for Subsequent Requests to the database
* */
public Connection getConnection()
{
try
{
//initialise the Driver for the database
Class.forName(driver);
//Establish Connection to the Database
connection = DriverManager
.getConnection(source, username, password);
} catch (ClassNotFoundException e)
{
System.out.println(e.getLocalizedMessage());
} catch (SQLException e)
{
System.out.println(e.getLocalizedMessage());
}
return connection;
}
/**
* @return Returns the driver.
*/
public String getDriver()
{
return driver;
}
/**
* @return Returns the password.
*/
public String getPassword()
{
return password;
}
/**
* @return Returns the resultset.
*/
public ResultSet getResultset()
{
if (resultset != null)
{
return resultset;
}
return null;
}
/**
* @return Returns the source.
*/
public String getSource()
{
return source;
}
/**
* @return Returns the username.
*/
public String getUsername()
{
return username;
}
/*Inserts a Record To the Database
* Accepts :a Query String
* Returns :No. of Rows Affected
* */
public int insertToTable(String insertQuery)
{
int res = 0;
try
{
System.out.println("Quey:" + insertQuery);
statemet = connection.createStatement();
//Insert a Record to the Database
res = statemet.executeUpdate(insertQuery);
if (res >= 1)
{
//Return No of Rows Inserted ( Count)
return res;
}
} catch (SQLException e)
{
System.out.println(e.getLocalizedMessage());
}
return res;
}
public ResultSet selectFromTable(String selectQuery)
{
try
{
System.out.println("Quey:" + selectQuery);
statemet = connection.createStatement();
//get a Result from the Database
resultset = statemet.executeQuery(selectQuery);
//Return the Result to the Calling Method
return resultset;
} catch (SQLException e)
{
System.out.println(e.getLocalizedMessage());
} finally
{
}
return null;
}
/*Initializes the Parameters to Connect to the Databse
* Accepts :Nil
* Returns :Nil (Sets the Current COnnection to the Database)
* */
public void setDbParameters()
{
String user = "";//Current User is Null
String pass = "";
setUsername(user);
setPassword(pass);
setDriver(DRIVER);
setSource(SOURCE);
setUsername(USERNAME);
setPassword(PASSWORD);
try
{
//Sets the Current COnnection to the Database
connection = getConnection();
if (connection != null)
{
System.out.println("Successfully Connected to the Database ! :"
+ connection.toString());
}
} catch (Exception e)
{
System.out.println(e.getLocalizedMessage());
}
}
/**
* @param driver The driver to set.
*/
public void setDriver(String driver)
{
this.driver = driver;
}
/**
* @param password The password to set.
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @param source The source to set.
*/
public void setSource(String source)
{
this.source = source;
}
/**
* @param username The username to set.
*/
public void setUsername(String username)
{
this.username = username;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -