mysql query using jdbc
- by S.PRATHIBA
Hi all,
I need to retrieve the last 20 values from my database.For example I have the following table
Service_ID Service_Type consumer_feedback
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 36 | Printer | 1 |
| 36 | Printer | 0 |
| 37 | Printer | 0 |
| 39 | Printer | -1 |
| 39 | Printer | 0 |
| 40 | Printer | 0 |
I need to retrieve last 10 values from the table.I need to do the operation using jdbc.I have attached the sample code.Kindly help me.
import java.io.;
import java.sql.;
public class CountRows2
{
public static void main(String[] args)
{
System.out.println("Count number of rows in a specific table!");
Connection con = null;
int count = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/prathi","root","mysql");
try{
Statement st = con.createStatement();
ResultSet res1 = st.executeQuery("SELECT COUNT(*) FROM consumer1" );
while (res1.next()){
count = res1.getInt(1);
}
System.out.println("Number of column:"+count);
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/prathi","root","mysql");
try{
Statement st = con.createStatement();
ResultSet res1 = st.executeQuery("SELECT * FROM consumer1 LIMIT count-10,10");
while (res1.next()){
int Service = res1.getInt(1);
String s1 = res1.getString(2);
int feedback=res1.getInt(3);
}
System.out.println("Service" + " " +"s1" + " " +"feedback");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
I am getting the output as:
C:javac CountRows2.java
C:java CountRows2
Count number of rows in a specific table!
Number of column:558
SQL statement is not executed!
Thanks a lot....