- You need to download the jdbc connector of mysql and java in http://dev.mysql.com/downloads/connector/j/
- Add this package to your libraries
- The information below is all about how to access the driver.
import java.sql.*;
import javax.swing.JOptionPane;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
StringBuffer output = new StringBuffer();
output.append("Tesing MySQL Driver installation");
try {
String className = "com.mysql.jdbc.Driver";
Class driverObject = Class.forName(className);
output.append("Driver: "+driverObject+"\n");
output.append("Driver Installation Successful");
JOptionPane.showMessageDialog(null, output);
}
catch(Exception x){
output = new StringBuffer();
output.append("Driver Installation FAILED\n");
JOptionPane.showMessageDialog(null,output);
System.out.println("Failed Driver Error:" +x.getMessage());
}
}
}
* on this part is an example of how to connect the mysql database using java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCDriverInformation {
static String userid = "root";
static String password = "";
static String url = "jdbc:mysql://localhost:3306/databaseapp";
static Connection con = null;
static String className = "com.mysql.jdbc.Driver";
public static void main(String[] args) throws Exception {
Connection con = getMySQLJDBCConnection();
if (con != null) {
System.out.println("Got Connection");
DatabaseMetaData meta = con.getMetaData();
System.out.println("Driver Name : " + meta.getDriverName());
System.out.println("Driver Version : " + meta.getDriverVersion());
} else {
System.out.println("Could not get connection");
}
}
public static Connection getMySQLJDBCConnection() {
try {
Class.forName(className);
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseapp", userid, password);
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
return con;
}
}
Hope you can run successfully in this given example.
No comments:
Post a Comment