Java
  Home arrow Java arrow Page 4 - Database Programming in Java Using JDBC
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Database Programming in Java Using JDBC
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2007-08-14

    Table of Contents:
  • Database Programming in Java Using JDBC
  • Accessing the Database using JDBC, Step by Step
  • Accessing the Database using JDBC continued
  • Using JDBC in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

       · In this article I have introduced the basics of JDBC. Please do comment
     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway