Home arrow Java arrow Page 4 - Database Programming in Java Using JDBC
JAVA

Database Programming in Java Using JDBC


An application that does not persist its data in a database is a rarity. Programming languages reflect this trend. That's why all languages provide a robust and flexible library for database access. Java is no exception.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 56
August 14, 2007
TABLE OF CONTENTS:
  1. · Database Programming in Java Using JDBC
  2. · Accessing the Database using JDBC, Step by Step
  3. · Accessing the Database using JDBC continued
  4. · Using JDBC in the Real World

print this article
SEARCH DEVARTICLES

Database Programming in Java Using JDBC - Using JDBC in the Real World
(Page 4 of 4 )

The time for theorizing is over. Now let's look at implementing what has been discussed. The application being developed has three classes:

  • GenericDAO - Connects to the database and provides a Statement object. It is generic in the sense that it accepts a driver name and URL as an argument of constructor.
  • DataOp - Implements database operations.
  • DAOTest - Tests the DAO and DataOp classes.

So here is the GenericDAO class. It accepts the driver class and URL to connect to as constructor arguments along with the user name and password.

package jdbctest;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class GenericDAO

{

Connection connection;

Statement statement;

public GenericDAO()

{

connection=null;

statement=null;

}

public GenericDAO(String driverClass,String connectionURL,String user,String password)

{

try

{

Class.forName(driverClass).newInstance();

connection=DriverManager.getConnection(connectionURL,user,password);

statement=connection.createStatement();

}

catch (InstantiationException e)

{

e.printStackTrace();

}

catch (SQLException e)

{

e.printStackTrace();

}

catch (IllegalAccessException e)

{

e.printStackTrace();

}

catch (ClassNotFoundException e)

{

e.printStackTrace();

}

}

public void setStatement(Statement statement)

{

this.statement = statement;

}

public Statement getStatement()

{

return statement;

}

}

Next is the DataOp class. It has one method that operates on the user table. This class is not generic.

package jdbctest;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

public class DataOp

{

Statement statement;

public DataOp(Statement statement)

{

this.statement=statement;

}

public List getUserList(String user)

{

List list=new ArrayList();

try

{

ResultSet result=statement.executeQuery("Select * from user where user_id='"+user+"'");

 

while(result.next())

{

list.add(result.getString(1));

}

}

catch (SQLException e)

{

e.printStackTrace();

list=null;

}

return list;

}

}

Last is the class that tests the GenericDAO and DataOp classes. Here we are passing the driver name corresponding to Type IV of MySQL JDBC driver and the corresponding URL.

package jdbctest;

public class DAOTest

{

public static void main(String args[])

{

//create instance of DAO class. Here we are using MySQL Type IV Driver

DAO dao=new

DAO("com.mysql.jdbc.Driver","jdbc:mysql://localhost/test","r

oot","root123");

//Creating instance of DataOp class

DataOp dataOp=new DataOp();

//calling the getUserList method for user whose id is 23

System.out.println(dataOp.getUserList("23"));

 

}

}

That completes a basic application. Also, it brings us to the end of this discussion. However, we have just scratched the surface of the iceberg called JDBC. In the future each aspect of the JDBC will be discussed in detail. Till then...


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials