Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=student.mdb;DriverID=22;READONLY=true
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at withoutdsn.Main.main(Main.java:26)
Java Result: 1
Asked
Active
Viewed 59 times
0
Marc B
- 340,537
- 37
- 382
- 468
Naomi Kulkarni
- 1
- 2
-
1in order to jdbc to work you should import the .jar in your program. – Karim Oct 14 '16 at 14:16
-
2Which Java Version do you use? – Jens Oct 14 '16 at 14:18
2 Answers
0
In order to successfully execute a connection with a database through java,for example if you are using mysql follow the steps below:
Go to mysql website and download the appropriate driver for Java. Then go to Project -> Properties -> Java Build Path -> Libraries (in Eclipse) and click on "add external Jars". Add the .jar you downloaded. Before doing anything else you should be sure to set your connection right.
For example:
//the default port of mysql is 3306
String url = "jdbc:mysql://127.0.0.1:3306/mydb";
String login = "root";
String passwd = "toor";
Connection cn = null;
Statement st = null;
ResultSet rs = null;
System.out.println("Connecting to database..");
try {
cn = DriverManager.getConnection(url, login, passwd);
System.out.println("Database Connected");
st = cn.createStatement();
String sql = "SELECT * FROM impacts";
rs = st.executeQuery(sql);
while (rs.next()){
//do something
}
}catch(Exception e){
System.out.println("Exception");
}finally{
if (cn != null ){
cn.close();
}
}
Ioanna Katsanou
- 330
- 1
- 4
- 22