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

 
 


moh sami
database connection by using java bean
1 Answer(s)      2 years and 3 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.

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 Pages:
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
bean object
bean object  i have to retrieve data from the database and want to store in a variable using rs.getString and that variable i have to use in dropdown... java.util.*; public class Bean { public List dataList(){ ArrayList list=new
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
connection
connection   how to make multiple database connection using jdbc
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
Bean
Bean  what is bean? how to use bean   HTML Code...        JDBC code: <%@ page language="java" import ="java.sql.*" %> <% Connection con; ResultSet rs; try { String strName
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
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
DataBase Connection
DataBase Connection  How to connect java and Oracle 10g? Tell me Jdbc connection
connection
connection   How to get connected to my database using jsp   Please visit the following links: http://www.roseindia.net/jsp/connectjspwith_mysql.shtml http://www.roseindia.net/jsp/displaydatafrom_database1.shtml http
Connection Pooling - JSP-Servlet
Connection Pooling  Hi All, Iam new to Connection Pooling in Java. I need to create connection pooling for my MySQL Database with JSP using Bean... ;;;;;;;;;;;; conpool.jsp ----------- //do something using con
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
A Message-Driven Bean Example
assign a message-driven bean?s destination during deployment by using... A Message-Driven Bean Example   ... messages asynchronously,  a Message-driven bean is used. Message driven
An Entity Bean Example
between Java objects and a relational database.   ..., such as relational database an entity bean persists across multiple session and can... and a database. For example, consider a bank entity bean that is used
can modify this application connection with database
can modify this application connection with database   hi i'm want... to Java Frame, in here can i ask one more question about using this application modify connection with database ? import javax.swing.*; import java.applet.
java bean - EJB
of the application.java bean is a ordinary java class using which we can develope...java bean  difference between java bean and enterprice java bean  first of all dont compare java bean with enterprise java bean because
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... to relating clients. There are several Database Connection Pools already available
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"
Chapter 13. Enterprise Bean Environment
: The enterprise bean's business methods access the environment using... Chapter 13. Enterprise Bean EnvironmentPrev .... Enterprise Bean Environment Identify correct and incorrect
REQ for connection b/w jdbc and oracle database
REQ for connection b/w jdbc and oracle database    REQ for connection b/w jdbc and oracle database    The Java classes to connect...:*** a) If you are using oracle oci driver,you have to use: Connection conn
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 - 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 - 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
database
database  I wanted to know if it is possible to establish database connection on a remote pc with mysql using java swings from netbeans and then create a database on the remote pc.... Kindly help me
How to create a jar file with database connection
How to create a jar file with database connection  Hello Everyone!!! Pls Help me in this... i have a program which has a database connection with ms... file using java. import java.io.*; import java.util.jar.*; public class
database
database  Im doing my project in java swings...netbeans I wanted java source code to establish connection to postgresql_8.1 using jdbc and create a user defined database and a table in that database and insert values Plz help me
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
Connect from database using JSP Bean file
`) ) In this example we have made a connection using Java Bean file named... Connect from database using JSP Bean file... = the name that refers to the bean. bean class = name of the java class
Database connectivity Hibernate mysql connection.
Database connectivity Hibernate mysql connection.  How to do database connectivity in Hibernate using mysql
JDBC Connection Pool
and see how to use Apache DBCP for creating the database connection pool. We... pooling. What is JDBC Connection Pool? The database connection is expensive... a new connection from the database for each user request is a huge work
java oracle connection problem - SQL
java oracle connection problem  I have successfully made connection with the oracle10g database that i am using and also able to put in data using... for fetching data from a oracle10g database.   Hi Friend, We have
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
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
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
Configure JMS connection factories and destinations
to the connection factory values you would fill in for any resource using... a connection factory, JMS destination, and deployed message-driven bean. When... Configure JMS connection factories
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
Ejb message driven bean
bean       This tutorial explains you the process which are involved in making a message driven bean using EJB...; For developing the message driven bean we are using both the EJB module and web
Connection pooling
database connections.   Hi, Connection pool stores a number of collection in a pool. This pool is called the Connection pool. In your Java program you can get the database connection and use and then close (returned to the pool
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
Interact with connection pools to obtain and release connections
in using a cached unshared connection. The possible drawbacks...) using the same data source or connection factory (same resource-ref... connection, even if you currently are not using it. Unlike a shareable
Bean
/UseBean.shtml http://www.roseindia.net/tutorial/java/jsp/usebean-setproperty.html

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.