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
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();
}
}
Are you using Java 8 and a JDBC-ODBC Bridge Driver? Bridge drivers are deprecated and not available in Java 8. See here.
If you want to run simple JDBC programs, instead of MS Access you could try using Java DB. Probably, Java DB works best with Netbeans IDE. There is nice tutorial here.
Related
I’m trying to connect a Java program to a remote Oracle DB. After doing some research online, I decided that the easiest way to do this was with the Oracle JDBC driver. I downloaded and ran the jar file and got the message “***** JCE UNLIMITED STRENGTH IS INSTALLED *****.” The problem is that when I try to add the driver to my classpath (javac -classpath ojdbc8.jar Connect.java), I keep getting an error message saying “package oracle.jdbc.driver does not exist.” I’ve been researching how to fix this online, but I’m only getting confused. Any ideas on what I did wrong?
import java.sql.*;
public class Class1 {
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You must put a database name after the # sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as `<host>`:`<port>`:`<sid>`. The example uses the short cut syntax.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:hr/hr#myhostname:1521:orcl",
"myUsername", "myPassword");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
conn.close(); // ** IMPORTANT : Close connections when done **
}
}
The error is:
java: package oracle.jdbc.driver does not exist
Can you try to run the sample DataSourceSample.java? Make sure you have the JDBC driver in the classpath. You can also refer to this quickstart for detailed instructions.
I am using eclipse and java to connect to an oracle 12c database. But every time I run through the process, I get a connection fail error that reads:
java.sql.SQLException: Invalid Oracle URL specified
Here is what my code looks like:
import java.sql.*;
public class JdbcConnection {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin#localhost:1521:xe", "username", "password");
Statement statement = con.createStatement();
String sql = "select * from employees";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
System.out.println(rs.getInt(3) + " " + rs.getString(2));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
I am using a windows 7 sp1 Ultimate (64 bit)
CPU: i5 2.85 ghz
RAM: 8GB
Oracle is installed in a separate hard drive (in the same computer).
EDIT:I have tried the first suggestion from this post: How do you find out the Oracle database's URL?
and I get an error that says:
ORA-00928: SELECT KEYWORD MISSING...
This error occurs when I run the oracle "SQL DEVELOPER" application on the startup menu and type the suggestions from post above.
Note: I want to connect to the database from eclipse and query it programatically.
Is there a way to find out what the correct connection URL is?
Please help.
I have a Jbutton (GetDataFromDB) in a simple java application that is suppose to load the data from the database depicted in the path in the code below into a Jtable in the application.
Edited answer into code:
private void GetDataFromDBActionPerformed(java.awt.event.ActionEvent evt) {
Connection con;
ResultSet rs = null;
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Driver={MS Access Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bruger\\Documents\\Database11.accdb");
stmt = con.createStatement();
String query = null;
query = "select * from cost";
rs = stmt.executeQuery(query);
i = 0;
while (rs.next()){
i = i + 1;
jTable.getModel().setValueAt(rs.getString(1), i, 1);
jTable.getModel().setValueAt(rs.getString(2), i, 2);
}
rs.close();
stmt.close();
con.close();
} catch(Exception err){
System.out.println(err.getMessage());
}
}
When I press the button I get the following message in the run output window:
No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb
I have at the top of my code the import:
import java.sql.*;
I have also tried changing from "Microsoft Access Driver" to "MS Access Driver" but I get the same message in the run output window i.e.
No suitable driver found for jdbc:odbc:Driver={MS Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb
I'm really thankful for all your help, input and feedback.
Depending on the driver and If you are pre JDK 6**!
You need to register the driver.
Try adding:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
So:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bruger\\Documents\\Database11.accdb");
It's also worth mentioning you don't need to do this every time you get a connection, just once to make sure the class is loaded.
There are a lot of stackoverflow questions relating to this, but the reason for it is below:
Source - From The Java Tutorial:
In previous versions of JDBC, to obtain a connection, you first had to
initialize your JDBC driver by calling the method Class.forName. This
methods required an object of type java.sql.Driver. Each JDBC driver
contains one or more classes that implements the interface
java.sql.Driver. ... Any JDBC 4.0 drivers that are found in your class
path are automatically loaded. (However, you must manually load any
drivers prior to JDBC 4.0 with the method Class.forName.)
On a related and very important note.
I would also recommend looking up how to handle connections. External Resources like database connections and cursors are easy to leak if you are not familiar with them. Look up 'try finally blocks', or more recently in java 7+ 'try-with-resources'.
I've this little code for connect my jsp to my mysql db.
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url="jdbc:mysql://localhost:3306/test1";
Connection con = DriverManager.getConnection(url, "test1", "test1");
Statement cmd = con.createStatement();
String query = "SELECT * FROM champions";
ResultSet res = cmd.executeQuery(query);
while (res.next()) {
System.out.print(res.getString("name") + ", ");
System.out.println(res.getString("title"));
}
res.close(); // chiudere le risorse DB è obbligatorio
cmd.close();
con.close();
I've added the connector to my build path:
But i'm still get this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Someone can help me?
Build path is for compilation purpose (assuming you don't have code to include mysql connector jar in package).
If this is web application, add it to lib folder.
If this is standalone application, make sure mysql connector jar available for runtime.
I have been assigned a task at work but i'm a beginner in Java and Eclipse.
I have to re-use an existing programm and modify some parts of it.
The following part does'nt work:
private java.sql.Connection conn;
private final String sqlRequest = "select ... from ...";
//................
private void DBConnect(){
try {
// Load the driver
java.lang.Class.forName(jdbcdriver).newInstance();
// Connect to database
conn = java.sql.DriverManager.getConnection(jdbcURL,dbuser,dbpwd);
//................
private void search2() {
try {
// create SQL statement
java.sql.Statement stmt = conn.createStatement(); //my line 135
java.sql.ResultSet rs = execRequest(stmt,sqlRequest);
//................
The error message in eclipse is :
"java.lang.NullPointerException at mon_prog.search2(mon_prog.java:135)"
This part used to work before... so is the problem due to my JDK version? I have JDK7u2
Get the connection first ...using..
Connection conn = DriverManager.getConnection(connection string);
Your conn seems null.
Have a look into great Vogella tutorial on how to connect to MySQL database from java program (IDE neutral way): http://www.vogella.de/articles/MySQLJava/article.html
Example steps to connect to your database:
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
Set breakpoints on the Class.forName() and getConnection() calls in DBConnect(), and on your line 135, then run it all through the debugger. I wouldn't be the least bit surprised if getConnection() doesn't get executed before you try to use the connection. (Been there, done that, got the source control commit.)
If that's not it, you may also want to double-check that you really don't have another variable that is shadowing your intended conn instance. "Go to definition" is very useful for this; just make sure you end up in the expected place.