Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners database connection by using java bean
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


moh sami
database connection by using java bean
1 Answer(s)      3 years and 7 months ago
Posted in : Java Beginners


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.


Advertisement
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
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 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
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 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
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"
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
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
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
DataBase Connection
DataBase Connection  How to connect java and Oracle 10g? Tell me Jdbc connection
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
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
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
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
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
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
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... String strDataSource = "java:comp/env/jdbc/HTHLogDB"; ic = new InitialContext
Java Bean
Java Bean  How to run java bean program in Netbeans
Java Bean
Java Bean  What is a Java Bean?   A Java Bean is a software component that has been designed to be reusable in a variety of different environments
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
Java Bean
Java Bean  What is Java Bean?   I got my answer already here: http://www.roseindia.net/jsp/usingbeansinjsp.shtml Thanks a lot
 

 

 

DMCA.com