Reputation: 1295
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.
Upvotes: 0
Views: 188
Reputation: 1504
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.
Upvotes: 0
Reputation: 15333
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
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")) ;
}
Close the connection, result set etc. in finally
block.
Your class name should be Tester
according to Java Naming Conventions.
Upvotes: 0
Reputation: 5306
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);
Upvotes: 1
Reputation: 2300
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.
Upvotes: 0
Reputation: 2113
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();
}
Upvotes: 0