Reputation: 59
public void actionPerformed(ActionEvent E)
{
int id;
String name,address,phone;
Connection conn = null;
PreparedStatement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
String sql;
sql = "SELECT * FROM person";
System.out.println("Creating statement...");
stmt = conn.prepareStatement (sql,ResultSet.TYPE_SCROLL_INSENSITIVE ,
ResultSet.CONCUR_UPDATABLE );
ResultSet rs = stmt.executeQuery(sql);
if(E.getSource()== bNext) {
rs.next();
id = rs.getInt("id");
name = rs.getString("name");
address = rs.getString("address");
phone = rs.getString("phone");
//Display values
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.print(", Address: " + address);
System.out.println(", Phone: " + phone);
}
if(E.getSource()== bPrevious) {
rs.previous();
id = rs.getInt("id");
name = rs.getString("name");
address = rs.getString("address");
phone = rs.getString("phone");
//Display values
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.print(", Address: " + address);
System.out.println(", Phone: " + phone);
}
if(E.getSource()==bLast) {
rs.last();
id = rs.getInt("id");
name = rs.getString("name");
address = rs.getString("address");
phone = rs.getString("phone");
//Display values
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.print(", Address: " + address);
System.out.println(", Phone: " + phone);
}
if(E.getSource()==bFirst) {
rs.first();
id = rs.getInt("id");
name = rs.getString("name");
address = rs.getString("address");
phone = rs.getString("phone");
//Display values
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.print(", Address: " + address);
System.out.println(", Phone: " + phone);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}
I don't know why, but the previous()
method is not working and whenever the next
button is pressed by the user, the output remains the same, and doesn't move forwards. can anyone help please?
Upvotes: 0
Views: 1600
Reputation: 393781
Each time your actionPerformed
method is called (I'm assuming it's called when some button is pressed), you execute the query again. Therefore next()
will always give you the first element and previous
will return false.
You should probably read all the rows into some data structure such as ArrayList
(as an initialization step, not when the button is clicked), and maintain the current index of that List
, which will allow you to move forward or backward when a button is clicked.
P.S. you shouldn't use rs.next()
or rs.previous()
without checking the returned value. If those methods return false
, you should not call methods such as rs.getInt("id")
, since they will throw an exception.
P.P.S some JDBC drivers don't support the previous()
, first
& last
methods, which means they might throw a SQLFeatureNotSupportedException
. However, you'll only need next()
if you read all the data in an initialization step, as I suggested.
Upvotes: 8