File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Database connectivity issue using JAVA and MySql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Database connectivity issue using JAVA and MySql" Watch "Database connectivity issue using JAVA and MySql" New topic
Author

Database connectivity issue using JAVA and MySql

Mayur Saparia
Greenhorn

Joined: Feb 13, 2013
Posts: 19

I want to create database connection in java using MySql. I have simple program which shows messsage went connected to mysql but the programs give error.

import java.sql.*;
import javax.sql.*;

public class MysqlConnect{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}

} //end main

} //end class


Error are
run:
MySQL Connect Example.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at MysqlConnect.main(MysqlConnect.java:13)
BUILD SUCCESSFUL (total time: 1 second)

please suggest me tutorial or help me to get the connectivity. I have read a tutorial where it says i need Jconnector i have downloaded that but i dont know how to use it OR where to place that JAR file.


Thanks In advance

Regards
Mayur.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
This is a JDBC tutorial. It is good.
For the MySQL connector, google for the MySQL handbook and there is a chapter about using the connector.
Don’t try it from a Swing application. Get it running at the command line and then put a Swing GUI on top of it.
rohan sethi
Ranch Hand

Joined: Dec 14, 2012
Posts: 34

The problem is in your JDBC driver's path.Code is fine.Are you using an IDE?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2584
    
    9

In your code, the lines

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);

should be

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection (dbUrl, username, password);

And of course will need the JDBC driver in the classpath in order to run/compile.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Mayur Saparia
Greenhorn

Joined: Feb 13, 2013
Posts: 19

Thank you everybody i could solve my problem with the help of all the solutions you posted for me.
Looking forward to come up with more question soon.
Once again thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

K. Tsang wrote:should be

Class.forName("com.mysql.jdbc.Driver").newInstance();

That used to be the case for some older versions, but the current MySQL driver has been improved to register itself during class loading instead of initializing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database connectivity issue using JAVA and MySql