aspose file tools*
The moose likes JDBC and the fly likes How to move cursor to next row? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to move cursor to next row?" Watch "How to move cursor to next row?" New topic
Author

How to move cursor to next row?

ramesh kumar
Ranch Hand

Joined: Nov 16, 2000
Posts: 32
Hi friends,
I am doing a assignment which creats a database, then takes
the database values during program execution, also known as
dynamic allocation. my problem is how to move the cursor or control after it reads the first row of values?
my working code is below. this pgm saves one row of data. HOw to save multiple rows? I've tried using loops. but still I think
I miss something. Pls help me.
thanks friends.

//program to creat table, to enter values from the user
//the values are written into the created table
import java.sql.*;
import java.io.*;
class MarksTable {
public static void main(String a[]) {

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection =DriverManager.getConnection("jdbcdbc:ramesh","","");
if(connection==null)
{ System.out.println("there is some error in connection"); }
Statement statement = connection.createStatement();
String def = "create table MARKS " +
"(NAME VARCHAR(32), " +
"PHYSICS VARCHAR(32), "+
"MATHS VARCHAR(32)) ";
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter student name");
String Name = br.readLine();
System.out.println("Enter physics marks");
String Physics = br.readLine();
System.out.println("Enter maths marks");
String Maths= br.readLine();
statement.executeUpdate(def);
String stri = "INSERT into MARKS (Name, Physics, Maths)" + "values(?,?,?)";
PreparedStatement stmt = connection.prepareStatement(stri);
stmt.setString(1, Name);
stmt.setString(2,Physics);
stmt.setString(3,Maths);
int row = stmt.executeUpdate();

statement.close();
connection.close();

}catch(ClassNotFoundException e){System.out.println("error");
}catch(SQLException ex) {System.out.println("errrorrrrrrr" + ex.getMessage());
}catch(IOException ee){System.out.println("IO Error");}
}
}
[This message has been edited by Thomas Paul (edited December 14, 2000).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You don't need to do anything with a cursor. You need to put your logic that reads from the screen and runs the PreparedStatement in a loop. You will also need some for the person entering the information to tell you when the last entry has been made (something like "end" in the name field, for example).


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
This situation is a very good cadidate for prepared (pre-compiled) statements. Just have ? ? ? in the places where your want to change the inputs and execute the SAME pre-compiled statement again which is FASTER.
regds
maha anna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to move cursor to next row?