chythlookcommunications.com

web design, programming and web maintenance

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

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

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

Related posts:

  1. Importing External Access Database Tables Using Visual Basic by Nicholas Brown Sometimes a Microsoft Access database user may find it desirable...
  2. A Guide To Java Programming Many older languages, like C and Pascal, were procedural languages....
  3. Creating Sql Database Connection in Asp.net Web Application Through C# 1: Open visual studio 2005 and from FILE select NEW->...
  4. Recover Dbf Databases Database damage can occur for a variety of reasons. As...
  5. The system cannot self repair this error – while accessing SQL database Database Console Commands (DBCC) check the logical and physical consistency...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply

TopOfBlogs