0

i have written my first program but in output i cant see any thing. i create my table in mysql.

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 


public class tester {

    public tester() {
    }
   public static void main(String[] args)  {


    String driver = "com.mysql.jdbc.Driver"; 
    String user = "root"; 
    String pass = "123456"; 

    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultSet = null; 
ResultSet rs;
    try { 
     Class.forName(driver).newInstance(); 
     connection = DriverManager.getConnection( 
        "jdbc:mysql://./test", user, pass); 
     statement = connection.createStatement(); 
     resultSet = statement.executeQuery("SELECT * FROM  student"); 
     rs = statement.getResultSet();
        while (rs.next()) {            
           System.out.print(rs.getString("sname")+("\t")) ;
           System.out.print(rs.getString("sfamily")+("\t")) ;
           System.out.print(rs.getString("saddress")+("\t")) ;
        }
     rs.close();
     statement.close();
     connection.close();

    } catch (Exception e) { 
}
   }
}

in my output i have: run: BUILD SUCCESSFUL (total time: 0 seconds) i cant see my records. what shall i do? i add mysql-connector-java-5.1.18 in my project.

5
  • 2
    It seems that you have build your program but didnt run it? Commented Feb 1, 2012 at 6:03
  • Do you have data in that table ? Commented Feb 1, 2012 at 6:04
  • why you're using two resultset object you can use only one resultset object to retrieving the data from the database, try with the rs = statement.executeQuery() and put next line in comment i.e. rs=statement.getResultSet(); Commented Feb 1, 2012 at 6:05
  • i have data in my table. and i run the program. Commented Feb 1, 2012 at 6:08
  • Please check if the connection has been established, if it has, i think GPS provides the correct answer. And, as naresh suggested, you shouldn't swallow the exception, just print the exception message. Commented Feb 1, 2012 at 6:18

5 Answers 5

1

First of all, you're not getting any exceptions because you're not printing them. Change this part:

} catch (Exception e) { 
}

to this:

} catch (Exception e) { 
    e.printStackTrace();
}

I suspect the problem would be with the connection string here:

connection = DriverManager.getConnection("jdbc:mysql://./test", user, pass);  
Sign up to request clarification or add additional context in comments.

Comments

0

Are you using some IDE like eclipse? Else, compile and run the program as following:

javac tester.java
java tester

And in your catch clause do the following, instead of completely ignoring the exception:

catch (Exception e){
e.printStackTrace();
}

2 Comments

i am using netbean. please help me. it is my first program in java. i am trying to learn.
go through docs.oracle.com/javase/tutorial/getStarted/cupojava/… to know how to build and run programs with netbeans IDE
0

hey I think this code works.

public static void main(String[] args) {

    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "";

    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;

    try {
        Class.forName(driver).newInstance();
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost/ums1", user, pass);
        statement = connection.createStatement();
        rs = statement.executeQuery("SELECT * FROM user");

        while (rs.next()) {
            System.out.print(rs.getString(1) + ("\t"));
            System.out.print(rs.getString(2) + ("\t"));
            System.out.print(rs.getString(3) + ("\t"));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            connection.close();
            rs.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

mind to change database url and table name. I tested with my one.

5 Comments

i didnot work. how can i be sure my program connect to mysql?
i add mysql-connector-java-5.1.18 to my project.
did you add the connector in your buildpath?
no, where is buildpath? i just add to library--- add jar files
are you using eclipse ? look this image : dl.dropbox.com/u/44236198/buildpath.PNG
0

Try with the following code

 Class.forName("com.mysql.jdbc.Driver");
 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",user,pass);
 statement = connection.createStatement();
 resultSet = statement.executeQuery("SELECT * FROM  student");
 while (resultSet.next()) {
        System.out.print(resultSet.getString("sname")+("\t")) ;
        System.out.print(resultSet.getString("sfamily")+("\t")) ;
        System.out.print(resultSet.getString("saddress")+("\t")) ;
 }

And ADD THIS to catch block

catch (Exception e) { 
      e.printStackTrace();
}

That's why you are not getting any exception.

Suggestions

  1. You don't need two different result sets. You can do this way too :

    resultSet = statement.executeQuery("SELECT * FROM  student");
    while (resultSet.next()) {
       System.out.print(resultSet.getString("sname")+("\t")) ;
       System.out.print(resultSet.getString("sfamily")+("\t")) ;
       System.out.print(resultSet.getString("saddress")+("\t")) ;
    }
    
  2. Close the connection, result set etc. in finally block.

  3. Your class name should be Tester according to Java Naming Conventions.

2 Comments

@sami are you using IDE or running program from console ? are you getting any exception while running it ?
@sami try to replace your code with the code I wrote in answer.
0

1.You dont need two resultset.You can go below code.

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

//</editor-fold>
/**
 *
 * @author mohsen
 */
public class tester {

    public tester() {
    }
   public static void main(String[] args)  {


    String driver = "com.mysql.jdbc.Driver"; 
    String user = "root"; 
    String pass = "123456"; 
    String dbUrl ="jdbc:mysql://localhost:3306/test1";

    Connection connection = null; 
    Statement statement = null; 
   /* ResultSet resultSet = null; */
ResultSet rs =null;
    try { 
     Class.forName(driver).newInstance(); 
     connection = DriverManager.getConnection(dbUrl, user, pass); 
     statement = connection.createStatement(); 
     rs = statement.executeQuery("SELECT * FROM  student"); 
   //  rs = statement.getResultSet();
        while (rs.next()) {            
           System.out.print(rs.getString("sname")+("\t")) ;
           System.out.print(rs.getString("sfamily")+("\t")) ;
           System.out.print(rs.getString("saddress")+("\t")) ;
        }
     rs.close();
     statement.close();
     connection.close();

    } catch (Exception e) { 

}
   }

}

2.Try to print Exception.So you got what is error in that.

3.Always close connection in finally.

1 Comment

It works dude i already tested and after i put it.You have to add one jar mysql.

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.