database connection by using java bean

Ads
 

database connection by using java bean

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.

View Answers

April 27, 2011 at 11:50 AM

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();
}

}

Ads









Related Tutorials/Questions & Answers:
database connection by using java bean
database connection by using java bean  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
JAVA DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
Advertisements
Draw graph using jsp without database connection
Draw graph using jsp without database connection  Draw graph using jsp code without database connection
Java Connection to Oracle database
Java Connection to Oracle database  HI I am ubable to connnect... type properly and the Test Connection failed.So Kindly tell me what to type in TNS Service name .ONe more thing is that i am using Oracle9i ,so how to delete
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array  i am using netbeans 7.0 ,with struts 1.3.8 and i want to insert multiple images in mysql database ,with use
java database connection
java database connection  package com.tsi.constants; public class Constant { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin
java database connection
java database connection  package com.tsi.constants; public class Constant { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin
About connection database oracle to java
About connection database oracle to java   I culdn't connect oracle datebase to java.when i compile the program it is ok. but when i clicked on sql... that connection is correct or not. The testing has failed .so what could be TNS
About connection database oracle to java
About connection database oracle to java  Hi I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked... to verify that connection is correct or not.The testing has failed .so what could
About connection database oracle to java
About connection database oracle to java  Hi I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked... to verify that connection is correct or not.The testing has failed .so what could
About connection database oracle to java
About connection database oracle to java   I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked on sql... that connection is correct or not.The testing has failed .so what could be TNS
About connection database oracle to java
About connection database oracle to java  **Hi I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked... to verify that connection is correct or not.The testing has failed .so what could
DataBase Connection
DataBase Connection  How to connect java and Oracle 10g? Tell me Jdbc connection
login form using java bean in eclipse
login form using java bean in eclipse  I have made a college community website but i need to implement beans to my login and contact us page. how can i do
Connection to Database
I manually make a connection to MySQL database in my web pages? How joomla... is the code how I am connecting to MySQL: I am connection to MySQL database in 2... database tables? Do I need to open one connection to each section? Thank you
database connection
database connection  hi all ready created database table using mysql database,employee payroll,attendance,employee details used database i need how to import and export excel file into database using jsp? pls help me any one
dyanamic upload of images using struts without database connection
dyanamic upload of images using struts without database connection  I want to upload images dynamically with out using data base..and want to create image for every user in my db
JDBC batch insert using Java bean class
In this tutorial, you will learn about JDBC batch insert using java bean / model class
database connection
database connection  how to connect the jsp page with database
database connection
database connection  i wanted to no how to connect sqlite database through netbeans? is it posible to connect it to a database that is on a remote pc? thank you
Database connectivity Hibernate mysql connection.
Database connectivity Hibernate mysql connection.  How to do database connectivity in Hibernate using mysql
please help me solve this problem when i am create database connection using servlecontext
please help me solve this problem when i am create database connection using servlecontext  hi... I have create a database connection using...("db", db); } //database connection class
Java Bean
Java Bean  How to run java bean program in Netbeans
connection with MySQL to java.
connection with MySQL to java.   how to connect MySQL database with jsp
Java Bean
Java Bean  What is Java Bean?   I got my answer already here: http://www.roseindia.net/jsp/usingbeansinjsp.shtml Thanks a lot
sending data to google chart api from mysql database using java
sending data to google chart api from mysql database using java  how to send data from mysql database to google chart api using java
error oracle database connection?
error oracle database connection?  hi now i am trying to connect oracle database and also insert my data into table, but it's not working.. I created...; PreparedStatement stmt=null; Connection con =null; ResultSet rs=null
ModuleNotFoundError: No module named 'database-connection'
ModuleNotFoundError: No module named 'database-connection'  Hi, My... named 'database-connection' How to remove the ModuleNotFoundError: No module named 'database-connection' error? Thanks   Hi
ModuleNotFoundError: No module named 'database-connection'
ModuleNotFoundError: No module named 'database-connection'  Hi, My... named 'database-connection' How to remove the ModuleNotFoundError: No module named 'database-connection' error? Thanks   Hi
connection
connection   how to make multiple database connection using jdbc
Regading Database connection
Regading Database connection  if we create the query table means how we retrive the query content from database

Ads