Reputation: 17474
I ran the following codes in Netbeans attempting to connect to MySQL database.
package database_console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class DBConnect
{
public static void main(String[] args)
{
try{
String host = "jdbc:mysql://localhost:3306/mydatabase";
String uName = "root";
String uPass = "password";
Connection con = DriverManager.getConnection( host, uName, uPass );
Statement stmt = con.createStatement();
String sql = "SELECT * FROM counselor";
ResultSet rs = stmt.executeQuery(sql);
rs.next();
int id_col = rs.getInt("id");
String first_name = rs.getString("firstName");
String last_name = rs.getString("lastName");
String p = id_col + " " + first_name + " " + last_name;
System.out.println(p);
}
catch(SQLException err){
System.out.println(err.getMessage());
}
}
}
I get the following exception message:
No suitable driver found for jdbc:mysql://localhost:3306/mydatabase
I know that I have to go to my project folder and add a JAR in my libraries folder. I've read through numerous online guides, however none talks about how to determine which JAR file is suitable.
So my question here is: Can I just get any JAR file and use it as a client driver? For example derbyclient.jar
? If not, is there any way to identify which JAR file is suitable to use as a client driver?
EDIT: Furthermore, I am a little confused whether I should download the required driver from Sun Microsystem
, Netbeans
, MySQL
.
Upvotes: 2
Views: 11826
Reputation: 421
I have encountered this error and this is how i solved it.
1)Go to Properties of your project
2)Go to Libraries
3)Add Library
4)Select MySQL JDBC Driver
This worked for me.
Upvotes: 3
Reputation: 41
Go to your netbeans->Project->Properties. In the libraries "Add Library". Select MYSQL JDBC Driver.
Upvotes: 2
Reputation: 64
You need to decide based on the Database that you have used.
In your case you have used MySQL so you need to use jdbc driver for MySQL. http://dev.mysql.com/downloads/connector/j/
If you are using Oracle then you can use jdbc driver provided by Oracle. http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
You can find jdbc driver on your chosen DB's official website. It is dependent on the JDK version as well as DB version.
Upvotes: 0
Reputation: 7079
You need to include a JAR in your classpath.
For Mysql, for e.g.- mysql-connector-java-5.1.18-bin.jar
If you are using maven, dependency in POM.xml would be
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
You need to register a driver, before getting connection using DriverManager.getConnection
as follows
Class.forName("com.mysql.jdbc.Driver");
Upvotes: 1
Reputation: 802
You need to know the kind of database you are connecting to while preparing the host url.
In the example pasted
String host = "jdbc:mysql://localhost:3306/mydatabase";
The string "mysql" indicates its a mysql database. Hence you need to use mysql client library.
Upvotes: 0
Reputation: 77234
The JDBC driver is a connector specific to the database. In your case, you'll need the MySQL JDBC connector.
Upvotes: 0