plz help me How to link Access data base with java code.

public class DataBaseCon {
public static void main(String a[]) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DataBase");
System.out.println("Sucessfully Connected ");
Statement st= con.createStatement();
ResultSet rs= st.executeQuery("Select empid,ename from emp");

while(rs.next()){
System.out.print(rs.getString(1));
System.out.println(rs.getString(2));
}
st.close();
con.close();

}
}

Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at databasecon.DataBaseCon.main(DataBaseCon.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

it throw this Exception...
help me if i do wrong some thing ..plz
Thanks

That does not look like a valid connection string. You need to use something closer to this

String connectString = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + projectDirectory + filename;
try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:Driver={Microsoft Access Driver "//makes it use access driver
                    + "(*.mdb, *.accdb)};DBQ= c:\\StudentDatabase.accdb";//gets database at its path
            con = DriverManager.getConnection(url);//creates connection 
Statement st= con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);       
String     SQL = "Select empid,ename from emp";
ResultSet rs= st.executeQuery(SQL);
 
while(rs.next()){
System.out.print(rs.getString());
System.out.println(rs.getString());
}
        } catch (SQLException e) {

            JOptionPane.showMessageDialog(rootPane, "An error has occured: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException ce) {
            JOptionPane.showMessageDialog(rootPane, "An error has occured: " + ce.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

hope that works, make sure your sql query works, and change database name from 'DBQ= c:\\StudentDatabase.accdb'

System.out.println(rs.getString(1));
System.out.println(rs.getString(2));

error correction in above code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.