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.

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

}









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
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
Connection to database - Java Beginners
Connection to database   HI I have just Started working on a Project on Jsp We hve some JSP Pages & trying to connect to our Database . We are using SQL server 2000 ? We are not able to connect our database Tables
java database connection - JDBC
java database connection  sir i want to join my project with MS access database. i am making my project in netbeans.please tell me the coding to do... the data into the MS Access database and retrieve the data from the database table
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
database connection in javascript - Java Beginners
database connection in javascript  hi, How can i connect the database and insert the data's by using javascript. thank u in advance. ragards, sakthi
java database connection - Struts
java database connection  how to connect a database in struts program?  First Add two jar file1> commons-dbcp-1.2.x.jar2> mysql...; value="jdbc:mysql:///database_name?autoReconnect=true"
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
DataBase Connection
DataBase Connection  How to connect java and Oracle 10g? Tell me Jdbc connection
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   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
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
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
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
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)  how do i implement connection pooling... static final String strDataSource = "java:comp/env/jdbc/HTHLogDB"; ic = new
database connection
database connection  how to connect the jsp page with database
J2ME with Database Connection - MobileApplications
J2ME with Database Connection  I'm using SonyEricssion Java ME SDk for CLDC with WTK2. Can u give me a sample program for Database Connection using MS-Access with MIDlet code
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
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 with database - JSP-Servlet
and the connection with the database using jsp code, I get exceptions that I have... with java code. Is there any other way to establish a connection with database in jsp... with the database by writing the program in java. Following is the code: import
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
how to close the connection object,statement object,resultssetobject when using the microsoft access a database - SQL
how to close the connection object,statement object,resultssetobject when using the microsoft access a database   Hi Friend, I am developing the webapplication using the jsp(business logice-jsp).backend is microsoft access
Database connectivity Hibernate mysql connection.
Database connectivity Hibernate mysql connection.  How to do database connectivity in Hibernate using mysql
JDBC batch insert using Java bean class
In this tutorial, you will learn about JDBC batch insert using java bean / model class
database connection - SQL
the tables. Thanks in advance.  Hi Friend, Do you want to use java-database connection in eclipse IDE? Thanks...database connection  hi friends, This is poornima i want
DataBase connection with sql - Struts
DataBase connection with sql  How to connect sql and send db error in struts? what are the tag should i code in struts-confic.xml
database connection in struts - Struts
database connection in struts  Connecting database in Struts  in my project 4 table 1.user register 2.login page 3.forgot password 4.welcome page. all in struts but i dont no how sql database connected in struts plz
Understanding Connection Object
with a database. When we connect to a database by using connection method, we create a Connection Object, which represents the connection to the database... to to establish the connection with the database. This is done by using the method
Connecting to the Database Using JDBC and Pure Java driver
engine we are using MySQL database server and MM.MySQL Driver.... In this search engine our application communicate with database using JDBC and for making connection we are using MM.MySQL JDBC Driver
Connect from database using JSP Bean file
`) ) In this example we have made a connection using Java Bean file named bean.java which... Connect from database using JSP Bean file... of the java class that defines the bean.  <jsp:setProperty name = ?id
Database Connection Pooling (DBCP)
and quicker response using the available connections as making new connection is very... the database connection for processing the request send by the clients... and returns to relating clients. There are several Database Connection Pools
server database connection - JSP-Servlet
server database connection  sir generally we will connect to the database by using the following steps 1.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver..." in that case how we will get the database connection   Hi Friend
Database Connection - JDBC
Database Connection  In java How will be connect Database through JDBC?  Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/jdbc-mysql/MysqlConnect.shtml Thanks
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
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
Regading Database connection
Regading Database connection  if we create the query table means how we retrive the query content from database
Database connection class in a live project ..??
Database connection class in a live project ..??  In a Live mvc project where should i put the database connection class ? I want to know in the best coding practice
android connection to database oracle 10g
android connection to database oracle 10g  Hello, How i can connect my android application to my oracle 10g database
connection database error
connection database error  import java.awt.EventQueue; // import packages import java.awt.event.ActionEvent; import java.awt.event.ActionListener...{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection
database backup and restore using java - JDBC
database backup and restore using java  Hi... I have MS Access 2000 database.I want to backup and restore the db using jdbc.What's the procedure for that. Thanks in advance
using a database to upload items - Java Beginners
using a database to upload items  I need to create a database... is my code and the items i need in a database import javax.swing.*; import... { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con
calling a session bean bean from servlet using netbeans - EJB
calling a session bean from servlet using netbeans  How to call a session bean from servlet using netbeans in Java
Using database - Java Server Faces Questions
Using database  Sorry,Actually I ant the project to register the personl details using mysql database and access by loging the user name and password in jsf,can you send me please?  My dear, You go the following url

Ads