0

The problem is when I run this code, I always get answer COL12 instead of getting all the columns with data like I am getting in MYSQL workbench. I need to get same values. Moreover when I use this query "DELETE FROM test WHERE col1 = 2;" in netbeans I get Error Exception.

Here is my Code I wrote in Netbeans.
package sams;

import java.sql.*;  
import java.sql.Statement;
import java.sql.DriverManager;




public class Temp_Class {



public static void main(String[] args){

    try {

String query="SELECT * FROM test";
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=      DriverManager.getConnection("jdbc:mysql://localhost/JavaProject", "root",  "19881990");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery(query);
        rs.next();
        String sname= rs.getString(2);
 System.out.println(sname);
con.close();
    } catch (Exception e) {

            System.out.println("Error");

      }


    }



}

I have made table test in MYSQL Workbench, with columns="col1, col2, col3" with values="1, COL11, COL12" and "2, COL21, COL22";

This is the code I use to delete a value in DB. . . but getting Error Exception.

public static void main(String[] args){

    try {


        String deletesql="DELETE FROM test WHERE col1 = 2";

        Class.forName("com.mysql.jdbc.Driver");
        Connection con= DriverManager.getConnection("jdbc:mysql://localhost/JavaProject", "root", "19881990");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery(select);

        Statement ds=con.createStatement();
        ResultSet dsrow=ds.executeQuery(deletesql);

        dsrow.next();
        String ds1= dsrow.getString(1);
        String ds2= dsrow.getString(2);
        String ds3= dsrow.getString(3);
        System.out.println(ds1+" "+ ds2+" "+ds3);

        con.close();
    } catch (Exception e) {

        System.out.println("Error");

    }


}
1
  • Can you show the code and the complete stacktrace? Commented Apr 1, 2015 at 10:48

4 Answers 4

2

String sname= rs.getString(2); returns one column from the resultSet. If you want to have all columns you have to use:

String sname0= rs.getString(0);
String sname1= rs.getString(1);
String sname2= rs.getString(2);
Sign up to request clarification or add additional context in comments.

9 Comments

String sname= rs.getString(1); String sname12= rs.getString(2); String sname13= rs.getString(3); System.out.println(sname+" "+ sname12+" "+sname13);
i wrote the above code now. now i am able to get the entire row. Can u please tell how can i get next row?? please
@user3615189 call rs.next();again and you get the next row.
Thanks alot Jens. Just last thing, DELETE query is also not working in netbeans. . . .plz little help
or see here for an example.
|
2

The reason you are getting one column is because you are fetching only one column

rs.next();
String sname= rs.getString(2);   //will give you one column

instead do

while(rs.next){                     //get all rows

 String col1= rs.getString(0);      //get column 1 of respective row
 String col2= rs.getString(1);      //get column 2 of respective row

 System.out.println(col1+" "+col2);

}

Comments

0

I think the problem is from rs.getString(2)
Here you have just tried to get the third column. You can use rs.getString(0) and rs.getString(1) to get other values.

Comments

0

test table

col1 col2 col3

col1     1        2
col2    col11   col21
col3    col12   col22

you are queries for col1 (i.e. SELECT * FROM test)

String sname= rs.getString(2);
System.out.println(sname);

rs.getString(2) i.e. we are passing column index i.e. second column col2 and it prints the first value in column 2 i.e. 1
here 1 should be printed in reality

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.