ms acces getting data problem(great problem for me but simple for you)

I can connect to ms access but i tried to get data from it following but i was uterly failes with several exceptions.

please tell me how can i select,update,run queries of the access database.give me some sample codes.

ResultSet rs = stmt.executeQuery("SELECT * FROM Table1");

System.out.println(rs.getString("Field1"));

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

[429 byte] By [chamila1986a] at [2007-11-27 6:06:30]
# 1

create a database named Books and design a table named Authors and try using this codes

//TableDisplay.java

// This program displays the contents of the Authors table

// in the Books database.

import java.sql.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class TableDisplay extends JFrame {

private Connection connection;

private JTable table;

public TableDisplay() {

// The URL specifying the Books database to which

// this program connects using JDBC to connect to a

// Microsoft ODBC database.

String url = "jdbc:odbc:Books";

String username = "anonymous";

String password = "guest";

// Load the driver to allow connection to the database

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 ); // terminate program

}

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 Authors";

statement = connection.createStatement();

resultSet = statement.executeQuery( query );

displayResultSet( resultSet );

statement.close();

}

catch ( SQLException sqlex ) {

sqlex.printStackTrace();

}

}

private void displayResultSet( ResultSet rs )

throws SQLException

{

// position to first record

boolean moreRecords = rs.next();

// If there are no records, display a message

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 {

// get column heads

ResultSetMetaData rsmd = rs.getMetaData();

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

columnHeads.addElement( rsmd.getColumnName( i ) );

// get row data

do {

rows.addElement( getNextRow( rs, rsmd ) );

} while ( rs.next() );

// display table with ResultSet contents

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.INTEGER:

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 TableDisplay app = new TableDisplay();

app.addWindowListener(

new WindowAdapter() {

public void windowClosing( WindowEvent e )

{

app.shutDown();

System.exit( 0 );

}

}

);

}

}

lordkurta at 2007-7-12 16:22:12 > top of Java-index,Java Essentials,New To Java...
# 2

Quoted from the API Javadocs (highlighting by me)

"A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. "

So your code...

ResultSet rs = stmt.executeQuery("SELECT * FROM Table1");

if(rs.next()){

System.out.println(rs.getString("Field1"));

}else{

System.out.println("Result set is empty"):

}

Also please don't use SELECT * that is a bad practice. Explictly name the columns you want to select.

cotton.ma at 2007-7-12 16:22:12 > top of Java-index,Java Essentials,New To Java...