Establishing Connection To Local MySQL Database
825700Jan 2 2011 — edited Jan 3 2011I am creating a java application and need to use a database. I've decided to use a local MySQL database. I am having difficulties establishing a connection to the database. I've analyzed various codes for accomplishing this on the internet and wrote my own code similar to the ones I've analyzed. I don't see why the code below doesn't work since I have also put the path to the connector inside the environment variable classpath. I've done everything that was suggested on various jdbc tutorial websites yet my code is not working. Any suggestions would be appreciated. I've already posted a thread on this issue previously but the suggestions made did not really help and I am hoping to get different results this time around.
These are the errors which I am getting:
Jan 2, 2011 6:10:22 PM homeworkorganizer.Database addCourse
SEVERE: null
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at homeworkorganizer.Database.addCourse(Database.java:28)
at homeworkorganizer.test.main(test.java:17)
Jan 2, 2011 6:10:22 PM homeworkorganizer.Database addCourse
SEVERE: null
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/homework
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at homeworkorganizer.Database.addCourse(Database.java:51)
at homeworkorganizer.test.main(test.java:17)
BUILD SUCCESSFUL (total time: 0 seconds)
Code I have:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package homeworkorganizer;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.*;
/**
*
* @author Nisha
*/
public class Database {
private String ipAdress= "localhost";
public void addCourse(String courseNumber,String courseCredit,String courseName) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
String server = "jdbc:mysql://"+ipAdress+"/";
String dbName = "homework";
String url = server + dbName;
Properties props = new Properties();
props.setProperty("user", "kman");
props.setProperty("password", "pass");
Connection conn;
Statement st;
String columns = "course_number, no_credits, course_name";
String sqlInsrt = "insert into course (" + columns + ") values ";
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/homework", "root", "kish");
System.out.println("Connected to the database\n");
st = conn.createStatement();
//Add user to the database
String userInfo= "('"+courseNumber+"','"+courseCredit+"','"+courseName+"')";
//Use executeUpdate when our statement will change the database.
st.executeUpdate(sqlInsrt + userInfo);
}
catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
}