What's wrong with my code

im trying using database with my application, but i have this error :

[Microsoft][ODBC Driver Manager] Invalid descriptor index

The query is like this : SELECT * FROM User

Which User table has 3 columns (id,name,level)

my java code is like this :

public ResultSet readData(String query){

ResultSet rs =null;

try{

openConnection();

stmt = conn.createStatement();

rs = stmt.executeQuery(query);

System.out.println("DBM : " + rs.getString(0));

}catch(Exception e){

System.out.println("Error : \n" + e.getMessage());

}

return rs;

}

privatevoid openConnection(){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection("jdbc:odbc:dsn_officework",

"","");

}catch(Exception ex){

System.out.println(ex.getMessage());

}

}

when i try to do :

System.out.println("DBM : " + rs.getString(0));

it throw an exception.

im using microsoft access 2003. the Data Source Name is fine.

is there something wrong with my code?

sorry for my bad english. im not too fluent.

[1933 byte] By [MyPermanaa] at [2007-11-27 11:21:38]
# 1

It appears your problem is here:

rs = stmt.executeQuery(query);

System.out.println("DBM : " + rs.getString(0));

You need this:

rs = stmt.executeQuery(query);

while(rs.next()){

System.out.println("DBM : " + rs.getString(0));

}

Also, your connection should use try/catch/finally block.

Here is an example. There are better examples on google:

Connection conn=null;

ResultSet rs=null;

Statement stmt=null;

try{

conn=datasource.getConnection();

rs = stmt.executeQuery(query);

while(rs.next()){

System.out.println("DBM : " + rs.getString("name of column"));

}

}catch(SQLException e){

e.printstacktrace();

} finally{

if(conn!=null)

conn.close();

}

George123a at 2007-7-29 14:49:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

This is my code :

import java.sql.*;

import javax.sql.*;

/**

* Class that handle database needs

* @author Permana

*/

public class DatabaseManager {

private Connection conn;

private Statement stmt;

private ResultSet rs = null;

/** Creates a new instance of DatabaseManager */

public DatabaseManager() {

conn = null;

stmt = null;

}

public Connection getConnection(){

return this.conn;

}

public Statement getStatement(){

return this.stmt;

}

private void openConnection(){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection("jdbc:odbc:dsn_officework",

"","");

}catch(Exception ex){

System.out.println(ex.getMessage());

}

}

public void closeConnection(){

try {

stmt.close();

conn.close();

if(rs != null){

rs.close();

}

} catch (SQLException ex) {

ex.printStackTrace();

}

}

/**

* Melakukan perintah query INSERT, UPDATE, dan DELETE.

* @param query perintah SQL yang akan dijalankan

* @return jumlah baris yang ditambah, diubah, atau dihapus.

*/

public int saveData(String query){

int val = -1;

try{

openConnection();

stmt = conn.createStatement();

val = stmt.executeUpdate(query);

}catch(SQLException e){

System.out.println("Error pada saveData, karena :\n" + e.getMessage());

}finally{

try{

conn.close();

stmt.close();

}catch(SQLException ex){

System.out.println("Error pada saveData, karena :\n" + ex.getMessage());

}

}

return val;

}

public ResultSet readData(String query){

try{

openConnection();

stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_UPDATABLE);

rs = stmt.executeQuery(query);

System.out.println("DBM : " + rs.getString(0));

}catch(Exception e){

System.out.println("Error pada readData, karena: \n" + e.getMessage());

}

return rs;

}

}

Im trying to make a class that will be handle all the database needs. Such as insert user, update, edit.

this is the way i use this class :

DatabaseManager dbm = new DatabaseManager();

// do the read data method

ResultSet rs = null;

String query = "SELECT * FROM User WHERE id='" + id+ "' AND password='" + this.password + "'";

rs = dbm.readData(query);

if(rs.next() == false){

System.out.println("ResultSet Empty");

throw new Exception("No user");

}else{

while(rs.next()){

id = rs.getInt("id");

}

}

dbm.closeConnection();

i have close the database connection. May be the other have class that i can use to handle all the database needs?

But, i'll give a try first. Thanks george123.

MyPermanaa at 2007-7-29 14:49:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

I try this code and it work fine

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn = DriverManager.getConnection("jdbc:odbc:dsn_officework");

Statement stat = conn.createStatement();

ResultSet rs = stat.executeQuery("SELECT * FROM User");

while(rs.next()){

System.out.println("isi data : " + rs.getString(2) + "\n");

}

}catch(Exception ex){

System.out.println("Error :" +ex.getMessage());

}

So there are somthing wrong with my DatabaseManager.java

Guess i have to write database code in the User Interface.

MyPermanaa at 2007-7-29 14:49:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...