connecting java to Mysql using Mysql-connector/j - java

I use netbeans 6.9 IDE and its features to connect my java class to Mysql database.
as you know, Mysql-connector driver is embedded in netbeans. I make new connection to Mysql database using Mysql(connector/j) driver, and every thing is okay, it displays all databases in Mysql and all tables in these databases, but when I create my java class to test the connetion and start manipulation, ClassNotFoundException is thrown when I call Class.forName("com.mysql.jdbc.Driver").newInstance();
ie. there is no driver ? why i got that exception ? the same happened when I connect to derby embedded db? can you help?
here my test class
public static void main(String[] args) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("driver true");
con = DriverManager.getConnection("jdbc:mysql:///test",
"root", "123456");
if (!con.isClosed()) {
System.out.println("Successfully connected to "
+ "MySQL server using TCP/IP...");
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}
}

You have to add the mysql connector .jar file (Libraries + Add Library + Mysql JDBC Driver) to your project.

On the left side Right-Click "libraries"->"Add Library..."-Select->"MySQL JDBC Driver" then rebuild the project.

You need to add the MySQL Connector/J library to the classpath of your project, otherwise Java will not be able to find the driver class file.

The answer is with your question. Driver is with your netbeans IDE and not with your java project. Add the required dirver (Jar file) to the project and run.

Related

How to use JDBC in IntelliJ IDEA?

I can't start "Hellow World, JDBC" app.
public static void main(String[] args) {
String username = "user";
String password = "pass";
String databaseUrl = "jdbc:mariadb://localhost:3306/example";
try{
Class.forName("org.mariadb.jdbc.Driver");
ClassLoader.getSystemClassLoader();
Connection connection = DriverManager.getConnection(databaseUrl, username, password);
} catch (ClassNotFoundException | SQLException throwables) {
throwables.printStackTrace();
}
}
I know then the error in this string. JVM can't find and load class by name, but idea seen this class.
I add mariadb connector as a jar lib. It means then idea will feed classpass to jvm when it compiling.
I also add requires org.mariadb.jdbc; in module-info.java , but I have the same error.
I've already tried creating new project. It's don't help me too. I really don't know why the JVM can't reached Driver class
The issue was in JDBC driver for mariadb v.2.7.2, 2.7.3, 2.7.4. I solved this problem by simply downgrading to an older version 2.4.4

Why I obtain this SQLException (No suitable driver) when I try to create a Connection object for an Oracle database?

I have to create a very simple batch Java application (an application that run into shell) and that perform some query on an Oracle database using JDBC and I never done it.
I am following this tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html
So I have done in this way to allocate a new Connection object for my application:
import java.sql.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
String partitaIVA = args[0];
String nomePDF = args[1];
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB
} catch(SQLException ex) {
ex.printStackTrace();
}
}
}
The problem is that when I run the application it seems that the SQLException is thrown because enter into the catch block and print the following error message in the console:
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Main.main(Main.java:16)
Why? What cause this problem and how can I fix this issue? What am I missing?
Tnx
You need to have an appropriate ojdbc.jar in you classpath. E.g. see Oracle JDBC ojdbc6 Jar as a Maven Dependency
The problem should be that the linked tutorial describes how to connect to a Mysql, but you're trying to connect to an oracle - therefore you need the oracle driver in your classpath.
How about following oracle's documentation for its Driver. http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
In the above code, you didn't registered the driver class.
Class.forName ("oracle.jdbc.OracleDriver");
If you are following instructions from the given link.
In chapter 2.2 you have instructions how to instal MySql drivers.
If you are using an Oracle database, then you'll need to instal Oracle JDBC drivers.
If you don't want affect your JDK installation like in tutorial, you can load driver dynamicaly

Microsoft Access 2007 connectivity in java 8 [duplicate]

This question already has an answer here:
Manipulating an Access database from Java without ODBC
(1 answer)
Closed 8 years ago.
I want to connect my database to msaccess 2007 using java, but I hear that the jdbc bridge is removed from java 8.
Please guide me that where is the problem in the following code.
import java.sql.*;
public class UserLogin
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// C:\\databaseFileName.accdb" - location of your database
String url = "JDBC:ODBC:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +
"C:\\Users\\Shakir\\Documents\\NetBeansProjects\\UserLogin\\me.accdb";
// specify url, username, pasword - make sure these are valid
Connection conn = DriverManager.getConnection(url);
System.out.println("Connection Succesfull");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
I hear that the jdbc bridge is removed from java 8.
Please guide me that where is the problem in the following code
The problem is precisely that the JDBC-ODBC Bridge has been removed from Java 8, so your code is trying to use a feature that is simply not available. Consider this approach instead:
Manipulating an Access database from Java without ODBC
ODBC Data Source in your Control Panel Settings and add new database with source.

Unable to add odbc5.jar in classpath

I want to connect to the oracle DB using Play Framework 1.2.5. FOr this I have modified the application.conf file as follows:
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=system
db.pass=tiger
Then I tried to add the driver i.e. classes12.jar/odbc5.jar but everytime when i try to run it, I am getting the exception:
Cannot connect to the database, Driver not found
For adding the jar file in Eclipse IDE, below are the steps I tried:
1) Added it in the lib folder (present under the root directory of my new application) and then added it to the java build path
2) Added it in the framework/lib folder (inside the downloaded framework folder) and then added it to the java build path
In both the cases, I am getting the above mentioned exception.
Also, Please list down the steps for connection to an oracle db, I am not able to find it anywhere in the documentation
EDIT
I am able to add the jar in the classpath, everything was fine except that I did not restarted the server once it failed to connect the jar.
I did this code for fetching some data from the database:
Connection conn = DB.getConnection();
PreparedStatement stmt = null;
System.out.println(conn);
try {
stmt = conn.prepareStatement("select dept_id from emp where emp_id = 11");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("Dept Id: " + rs.getInt("dept_id"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This approach is working but I am having a confusion:
If I comment the entire block of code and run the application then I can see the message in the console stating the connection is made to the DB. Hence :
1) Is the above block of code the right approach to fetch the data from Oracle DB or something better than this is present?
2) Is it like for the entire application lifetime, the connection with tht DB will persist?
I am a newbie in this, hence struggling :(
Please let me know hoe to proceed with this.
Regards
Oracle db driver class name is oracle.jdbc.driver.OracleDriver

Get mysql and java to work together

I have Eclipse Indigo on Mac Os X and I have downloaded mysql connector (version 5.1).
I have added the jar to the project and I am using this code:
public class Test
{
public static void main (String[] args)
{
try {
String url = "jdbc:msql://200.210.220.1:1114/Demo";
Connection conn = DriverManager.getConnection(url,"","");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
while ( rs.next() ) {
String lastName = rs.getString("Lname");
System.out.println(lastName);
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
When I try to execute the program I get this exception:
Got an exception!
No suitable driver found for jdbc:msql://200.210.220.1:1114/Demo
My question is : how to install the driver? What else should I do?
And once installed the driver, how do I get my database URL (I am using mysql 5.5)?
I haven't found a valid guide on the web, they're all too specific.
Your JDBC connection URL is not correct, refer to the official documentation to check the required format for the URL .
In your case the URL will become :
String url = "jdbc:mysql://200.210.220.1:1114/Demo";
you're missing the "y" in jdbc:mysql
You are using MySQL, the URL should look like this:
jdbc:mysql://200.210.220.1:1114/Demo
may be, review the IP and PORT.
You may have added the jar to your project but have you also added it to the project classpath? Having the jar exist as a file in your project won't solve the problem. The jar file is clearly not accessible to your program. Right click on your project -> Build Path -> add the jar there.
Database URL looks ok, assuming you have the right host address and port number.

Categories

Resources