i need a code for bean class to connect to mysql database. subsequently to use dis bean class whereever i need 2 connect 2 database.
package com.student.database;
// Import Statements
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;
public class DatabaseOperation // A class to perform all the database Operations {
static final String DbDriver = "com.mysql.jdbc.Driver"; // To set the driver
static final String strConnection = "jdbc:mysql://localhost:3306/student"; // Connection string
static final String strDbUsername = "root"; // Username of the database user
static final String strDbPassword = "root"; // Password of the database user
static Connection DatabaseConnection; // Connection object to establish the connectin to the database
static ResultSet RsStudent; // Resultset to store the information retrieved from the database
static Statement DatabaseStatement; // Statement object of the database connection
public DatabaseOperation () // Constructor of the class
{
makeConnection(); // To establish the database connection
}
public void makeConnection() // To establish the database connection, No return value and parameters
{
try
{
Class.forName(DbDriver); // Setting the driver
DatabaseConnection = DriverManager.getConnection(strConnection, strDbUsername, strDbPassword); // Establishing the connection with the database
DatabaseStatement = DatabaseConnection.createStatement(); // Assigning the statement to the connection
System.out.println("Connection Success....");
}
catch (Exception e) // In case of any Exception
{
System.out.println(e); // Print the Exception
}
}
public void insertIntoDatabase(String strInsertQuery) // A function which inserts the datavalues into the databases, No return value, Query as Parameter
{
boolean IsResult = false; // To test whether the record is inserted
try
{
IsResult = DatabaseStatement.execute(strInsertQuery); // Execute the insert Query
//DatabaseConnection.commit(); // Commit
}
catch (Exception e)
{
System.out.println(e); // Incase of any error print the error
}
}
public void updataDatabase(String strUpdateQuery) // A function which updates the changes into the database, No return type, Query as parameter
{
try
{
int row = DatabaseStatement.executeUpdate(strUpdateQuery); // Returns No of rows affected
}
catch (Exception e)
{
System.out.println(e); // In case of errors, print the error
}
}
public ResultSet selectFromDatabase(String strSelectQuery) // A function which retrieves Records from the database, Returns Recordset, Query as parameter
{
try
{
RsStudent = DatabaseStatement.executeQuery(strSelectQuery); // Execute the select query
return RsStudent; // Returns the resultset
}
catch (Exception e)
{
System.out.println(e); // Print the exception
return null; // Return NULL
}
}
public void deleteFromDB(String strDeleteQuery) // A function Which is used to execute delete Queries, No Return type, Query as Argument
{
boolean IsReturn = false; // To check Whether the Query is appropriately Executed
try
{
IsReturn = DatabaseStatement.execute(strDeleteQuery); // Executing the Query
}
catch (Exception e)
{
System.out.println(e); // Incase of Error, Print the error
}
}
public void disConnect() {
try {
DatabaseConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
public static void main(String[] args) {
DatabaseOperation db = new DatabaseOperation();
db.makeConnection();
}
}