0

I'm not good with Java I've only learned the basics and it's still kind of fresh somehow. I want to know how does someone print a row from a table to the Java Console.

try {
    statement = con.prepareStatement(
                "SELECT * FROM users WHERE Username ='" + 
                LOGIN_UsernameField.getText() + "' AND Password ='" +
                LOGIN_PasswordField.getPassword() + "'");
    System.out.println ();
}

That system.out.println thing I know that doesn't work of course, I need something to replace it to print out the rows that comes from the SQL Statement right above it.

Hiroga Katageri
  • 1,345
  • 3
  • 21
  • 40

2 Answers2

2

Try this tutorial from Oracle. It is simple and straight forward.

It would be something like this

try{
        statement = con.prepareStatement(
                "SELECT * FROM users WHERE Username = ? AND Password =?");
        statement.setString(0,LOGIN_UsernameField.getText());
        statement.setString(0,LOGIN_PasswordField.getPassword());
        ResultSet rs = statement.execute();
        while(rs.next()){
           System.out.println("UserName:"+rs.getString("Username")+"\nPassword:"+rs.getString("Password"));
       } 
}catch(){
 //handle exceptions
}finally{
 //close statements, statement and resultset here..
}
Richie
  • 9,006
  • 5
  • 25
  • 38
  • 1
    Thank you, thank you, I actually tried bing and google, I just well couldn't find the right place, I used wrong keywords. Anyway thank you again. – Hiroga Katageri Jul 19 '13 at 06:19
2

Here is the code

    System.out.println("-------- MySQL JDBC Connection Testing ------------");


    Connection connection = null;

    try {
        connection = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/testDb", "userName", "Password");
    } catch (SQLException e) {
        for(Throwable ex : e) {
            System.err.println("Error occurred " + ex);
        }
        e.printStackTrace();
    }

    if (connection != null) {
        System.out.println("Connected to database!");
    } else {
        System.out.println("Failed to make connection!");
    }

    try {
        Statement stmt = connection.createStatement();
        String query = "select * from person ;";
    //person is the table name
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String name = rs.getObject(1).toString();
            String gender = rs.getObject(2).toString();
            System.out.println("Name of the person is " + name + " and his gender is " + gender);
    //Person table has name and gender column

        }
    } catch (SQLException e) {
        e.printStackTrace();
        for(Throwable ex : e) {
            System.err.println("Error occurred " + ex);
        }
        System.out.println("Error in fetching data");
    }

Note : I am using JDBC 4 that come with Java 7 and hence no need to explicitly provide driver. Compiler will take it automatically from the class path. You can download MySqlConnector.jar and put it in your classpath.If you are not using java 7 you will have to explicitly load the driver using Class.forName(). And of course I am using MySql... the driver will change with the database you use.

Hope this helps.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289