Free WebSchools.com

Google




Blog ForumsLive chat WebhostingLink TO UsLink TO UsSEO Tools


HOME

J2ee tutorials

Java introduction
Java basic
Java installation
Java Packages
Learn Applets
Java Threads
Java Gui component
pixels


Java Event Handaling
Java Streams and Files
pixels
Java Swings
Java JDBC
pixels
Java Network Programming
Java RMI
pixels
Java Servlets
Java javabeans
pixels
EJB
Struts
pixels


java JDBC



Data are raw facts from which information can be derived. A database is a record-keeping system of an organization, more specifically a computer-based system, the purpose of which is to record, and store data. A database can be defined as a collection of stored operational data used by the application systems of some particular enterprise

A database is collection of interrelated data from which some information can be extracted. The collection of data must be logically coherent with some internet meaning. Database is designed and built for specific purpose, keeping in mind the needs of the applications that are going to use it and the end user of those applications. Ad database is like data file, a data storage place.



A database typically has two components the files holding database and the database management system (DBMS) software that application to use to access, format, and output data. a DBMS is general purpose software system that enables users to define And manipulate database. it provides an environment wherein data can be retrieved from database easily and efficiently.

Database are classified into three type

Hierarchical (it has tree structure and relationships are typically one-to-one)

Network (multi-relationship can be established in the networks model)

Relational (in this data is organized in rows and columns)

JDBC is the first standardized API that allows users to create robust, platform-independent applications and web-based applets, which can access any database through the DBMS independent mechanism. It is implemented through the java.sql package, which contains all the JDBC classes and methods required by a developer.

The JDBC API has two levels of interface: the application Layer and the Driver layer

The application layer allows the developer to make calls to the database via SQL and retrieve the results.

The driver layer handles communication with a specific driver implementation.



A relational database management system (RDMS) Is a database based on the relational model invented by E.F Codd at IBM IN 1970/ it allows the definition of data structures, storage and retrieval operations and integrity constraints.

Program to retrieve the result from a table


The following program executes a sql select statements to list all the employees in the database
Import java.sql.*;
Class sqldemo
{
public static void main(String a[]) throws Exception
{
String stno,stname;
Connection Conn;
Statement stmt;
ResultSet rs;
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

//to get a database connection
Conn=DriverManager.getConnection(“jdbc:odbc:empDB”, “username”,”password”);
//to create a statement object to execute the sql statement
Stmt=Conn.createStatement();
//sends the sql statement to the DBMS and executes
rs=Stmt.executeQquery(“select empno,empname form emp);
//processing the results
System.out.println(“empno  “+” name “);
While(rs.next())
{
stno=rs.getInt(1);
stname=rs.getString(2);
System.out.println(stno+”\t”+stname);
}
}

While creating the above program the following steps have been followed

Step 1: the program initialized and loads the JDBC-ODBC driver using the static method forname() of the class Class

Step 2 the program calls the getConnection() method to get the connection object

Step 3 with the help of the object created in step 2, it involves the method createStatement() to create the statement object and prepares SQL statement

Step 4 with the help pf the statement object, you than invoked the execiuteQquery() method of the statement interface, passing it the select empno and empname form emp SQL statement as an argument. This resulted in the query being processed by the database and the ResultSet object being returned.

Step 5 a ResultSet object maintains a pointer to row within the tabular results. The pointer is referred to as cursor. when a ReultSet object is returned from a query, the cursor initially points immediately before the first row of the table the next() method returns a Boolean value that is true if the row is returned and false if the end of the table is reached. Hence each row id processed until there are no more rows

Program to update the table


Import java.sql.*;
Class JDBCupdatedemo
{
void updatename(String str)throws SQLException,ClassNotFoundException
{
int RetValue;
Connection Con;
Statement stmt;
String SQLBuffer;

//inoitalize and load the JDBC-ODBC driver
Class.forName(“sun.jdbc.odbc:empdb”,”username”,”password”);

//create a simple statement object
stmet=Conn.createStatement();

//make sql string,pass to dbms and execute the statement
SQLBuffer=”UPDATE emp SET deptno=50,job=’manager’ where ename=’”+str+”’”
RetValue=stmt.executeUpadte(SQLBuffer);
System.out.println(“number of rows update in the database is +RetValue);
}
}
public class JDBCupdatre
{
public static void main(String args[]) throws
SQLException,ClassNotFoundException
{
JDBCupadtedemo upd=new JDBCupadtedemo();
Upd.UpdateName(“ALLEN”);
}
}


In the above code the UpdateName() method has been created, which updates the deptno and job of particular, whose is passed as an argument

Inform Friend About This Site

Your Name:

Friend's Name:

Friend's Email: