Remember Me
forgot your password?

Java Programming - Accessing Databases Using Jdbc

JAVA PROGRAMMING – ACCESSING DATABASES USING JDBC

You can access databases using JAVA. The special feature available in Java for this purpose is Java Database Connectivity (JDBC).

You have to create a database in MS Access or Oracle or MySQL or any other database management system. Then give an ODBC name for your database.

Visit

http://learnjavatoday.blogspot.com

For more details

// The following example illustrates how to display a table in
// JAVA using
// Java Database Connectivity (JDBC).
// Before running the program, create a table
// named as BOOKS in Oracle 9i.
// Declare the database giving ODBC Name bk.
// User Name is kt.
// Password is kt.
// Save File as MyTable.java
// Assuming that your java bin directory is C:javabin
// Compile the file using
// C:javabin javac MyTable.java
// to create MyTable.class file.
// Execute the program using
// C:javabin java MyTable.

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MyTable extends JFrame {
private Connection connection;
private JTable table;

public MyTable()
{
String url="jdbc:odbc:bk";
String username ="kt";
String password="kt";

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url,username,password);
}

catch(ClassNotFoundException cnfex) {
System.err.println("Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}

catch( SQLException sqlex ) {
System.err.println("Unable to connect");
sqlex.printStackTrace();
}

getTable();

setSize(450,150);
show();
}

private void getTable()
{
Statement statement;
ResultSet resultSet;

try {
String query = "SELECT * FROM BOOKS";
statement = connection.createStatement();
resultSet=statement.executeQuery(query);
displayResultSet(resultSet);
statement.close();
}

catch ( SQLException sqlex) {
sqlex.printStackTrace();
}
}

private void displayResultSet(ResultSet rs) throws SQLException
{
boolean moreRecords=rs.next();
if (!moreRecords) {
JOptionPane.showMessageDialog(this,"ResultSet contained no records");
setTitle("No records to display");
return;
}

setTitle("Authors table from Books");

Vector columnHeads=new Vector();
Vector rows=new Vector();

try{
ResultSetMetaData rsmd=rs.getMetaData();

for ( int i=1; i<=rsmd.getColumnCount();++i)
columnHeads.addElement(rsmd.getColumnName(i));

do{
rows.addElement(getNextRow(rs,rsmd));
} while (rs.next());

table=new JTable(rows,columnHeads);
JScrollPane scroller=new JScrollPane(table);
getContentPane().add(scroller,BorderLayout.CENTER);
validate();
}

catch(SQLException sqlex) {
sqlex.printStackTrace();
}
}

private Vector getNextRow(ResultSet rs,ResultSetMetaData rsmd) throws SQLException
{
Vector currentRow= new Vector();

for (int i=1; i<=rsmd.getColumnCount();++i)
switch(rsmd.getColumnType(i)) {
case Types.VARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.DECIMAL:
currentRow.addElement(new Long(rs.getLong(i)));
break;
default:
System.out.println("Type was: "+rsmd.getColumnTypeName(i));
}
return currentRow;
}

public void shutDown()
{
try{
connection.close();
}
catch(SQLException sqlex) {
System.err.println("Unable to disconnect");
sqlex.printStackTrace();
}
}

public static void main(String args[])
{
final MyTable app=new MyTable();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
app.shutDown();
System.exit(0);
}
}
);
}
}



// For more details see Java How To Program

KULAPPU THANTHRIGE KARUNADASA

The author is holding a M Sc in Information Technology

Rate this Article: 0 / 5 stars - 0 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Programming Articles
  • More from KULAPPU THANTHRIGE KARUNADASA

Ajax - What Is It and How It Works

By: Elias Rizos | 16/12/2009
Ajax stands for Asyncronous JavaScript and XML and is known for making web applications function as fast (or faster) than desktop applications. Let's talk about what a typical web application looks like. For instance, a Google search:

Benefits of Professional SEO Services

By: Agile Axis | 16/12/2009
Search Engine Optimization (SEO) helps to promote a website over Internet. The work of SEO is to increase the quality and quantity of visitor for a particular site.

Where to hire a professional and expert PHP programmer?

By: Agile Axis | 16/12/2009
Hypertext processor programming (PHP) is the most commonly used web-scripting program nowadays. This software was developed in 1995. Originally PHP stood for Personal Home Page.

Software Consulting in India

By: Agile Axis | 16/12/2009
In this world of relentless competition, if you fail to provide service by the committed time, your business will have to suffer great loss.

Introduction Overview to SharePoint Development

By: David | 16/12/2009
The resources needed to run SharePoint on a devleopment server requires planning and this article will highlight the requirements in setting up SharePoint Development environment

Magento Module Development

By: Maulik Shah | 16/12/2009
Magento is a new and powerful eCommerce system offering rich customization possibilities by extensions and modules. Magento is built on a fully modular model that influences an unlimited scalability and flexibility for your store. Part of customizing Magento is, creating Magento custom Modules.

Ajax Development Services: Impressive, Innovative, Feasible

By: Arun Kumar | 16/12/2009
In the ever-evolving web world, pace is the name of the game. Perceptibly, there has been a consistent search to craft speedy and practicable solutions, which can add more momentum to web developments. Innovative solutions are plenty, but the most important thing about any web tool or solution that makes it popular is that it should be feasible in a longer run.

What is Typo3?

By: Typo Techie | 15/12/2009
TYPO3 is a free development framework for Web sites, which was originally developed by Kasper. TYPO3 is based on the PHP scripting language. As a MySQL database, but can also be used as PostgreSQL or Oracle. The system is powered by two teams, one for each version 4 and one for version 5, developed.

Java Programming - Accessing Databases Using Jdbc

By: KULAPPU THANTHRIGE KARUNADASA | 17/11/2007 | Programming
JAVA can be used to access databases. JAVA has a special feature for this purpose. called as JDBC. The artcle explains how to program for JDBC

Java Programming

By: KULAPPU THANTHRIGE KARUNADASA | 13/11/2007 | Programming
Java is a programming technology developed by Sun Microsystems. Java is an object-oriented language. Java can be used for web development. Java is platform independent. The java code is compiled into a bytecode that can be run on a Java Virtual Machine(JVM). So Java code will run on any system that has a JVM installed in it. Java is robust, flexible and scalable. It can also be used to communicate with legacy systems.

Is your Computer Running Slow

By: KULAPPU THANTHRIGE KARUNADASA | 10/11/2007 | Hardware
Your computer may be running slow due to various reasons. It may be due to spy ware, virus infection, low RAM, too many applications, registry errors, automatic updates etc.

Programmable Logic Cobtrollers

By: KULAPPU THANTHRIGE KARUNADASA | 04/11/2007 | Hardware
A programmable logic controller (PLC), is a digital computer used for automation of industrial processes., The PLCs are widely used in indutrial automation.

Travel in Sri Lanka

By: KULAPPU THANTHRIGE KARUNADASA | 09/09/2007 | Travel
SRI LANKA IS ONE OF THE MOST BEAUTIFUL ISLANDS TO SPEND YOUR HOLIDAY

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.86, 6, w2)