I'm learning database with Java using JDBC. I've created a svery basic program after reading some articles on JDBC
on the internet. My code is not giving any error but I'm not getting the output also. Here's is my code:
This is my connectivity code: DataService
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataService {
static Connection con;
public static void main(String args[]){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","scott");
}
catch(Exception e){
System.out.println(e);
}
}
public ResultSet getRecords() {
System.out.println("inside getRecords()");
ResultSet rs=null;
try {
System.out.println("Inside Rs loop");
Statement stmt=con.createStatement();
rs=stmt.executeQuery("select * from emp");
System.out.println("RS before entering while: "+rs);
while(rs.next())
System.out.println("Inside while loop");
System.out.println("Data: " +rs.getInt(0)+" "+rs.getString(1));
} catch (SQLException ex) {
Logger.getLogger(DataService.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Before sending: "+rs);
return rs;
}
}
And here's is my application MainFrame.java
public class MainFrame extends Application {
...
@Override
public void start(Stage primarystage) {
...
try {
System.out.println("Inside try");
DataService dataservice = new DataService();
System.out.println("Result set: "+dataservice.getRecords());
} catch (Exception e) {
//TODO: handle exception
}
}
}
I'm getting this output:
Inside try
inside getRecords()
Inside Rs loop
I cant see any output related to records. Also I've checked select * from emp
. There are rows in that table already.
I'm missing out something very important here. Please correct me.