• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to move cursor to next row?

 
ramesh kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic