vendredi 8 juillet 2016

Java Database Connection not working

I connected my access database to a java application but somehow i cannot get the program to display the records in the database This is the piece of code i tried. It runs perfectly but it doesnt display the records please help

package Database;

 import java.sql.*;

public class db {

Connection con;//get connection to database
Statement st;//hava access to tables
ResultSet rs;//go through records

public db(){
   connect(); 

 }
    public void connect(){
    try{
        String driver="sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String db="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Users\Hp\studies\sample programs\java\DatabaseConnection\Java.mdb";
        con=DriverManager.getConnection(db);
        st=con.createStatement();
        String sql="SELECT * FROM Table1";
        rs=st.executeQuery(sql);

        while(rs.next()){
            String fname=rs.getString("Fname");
            String lname=rs.getString("Lname");
            String age=rs.getString("Age");

              System.out.printf("%st%st%s",fname,lname,age);
        }
    }
    catch(Exception ex){

    }


}
public static void main(String args []){
new db();
}

}

Aucun commentaire:

Enregistrer un commentaire