Topic: Can NOT connect to Mysql
Hi All
Below is my code for a simple form which connects to Mysql. Problem is if I just connect to database and not have any other code (i.e Form) that works fine but when I try to connect with the form code, it gives me errors. why??? some help will be greatly appreciated thank you.
Zed
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Driver;
public class PersonalInfo extends Frame {
Connection conn=null;
Panel Np = new Panel();
Panel Sp = new Panel();
public PersonalInfo(){
NorthPanel();
add(Np,BorderLayout.NORTH);
SouthPanel();
add(Sp,BorderLayout.SOUTH);
}
public void NorthPanel(){
Label LfirstName = new Label("First Name");
TextField TfirstName = new TextField(20);
Label LlastName =new Label("Last Name");
TextField TlastName=new TextField(20);
Np.setLayout(new GridLayout(2,1));
Np.add(LfirstName);
Np.add(TfirstName);
Np.add(LlastName);
Np.add(TlastName);
}
public void SouthPanel(){
Button Submit=new Button("Submit");
Sp.add(Submit);
Button Cancel = new Button("Cancel");
Sp.add(Cancel);
}
public static void connect(){
System.out.println("MySQL Connect Example");
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String userName = "c3031843";
String password = "vw84a3";
String driver = "com.mysql.jdbc.Driver";
try {
Statement stmt;
ResultSet rs;
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("URL: " + url);
System.out.println("Connection: " + conn);
System.out.println("Connected to the database");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * " + "from category");
System.out.println("Display all results:");
while(rs.next()){
int Int= rs.getInt("cat_id");
String name = rs.getString("category");
System.out.println("\tid= " + Int + "\tstr = " + name);
}//end while loop
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String[] args) {
System.out.println("Personal information");
PersonalInfo PerInfo = new PersonalInfo();
PerInfo.setTitle("Personal Information");
PerInfo.setSize(400,200);
PerInfo.setVisible(true);
connect();
PerInfo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Error code
Personal information
MySQL Connect Example
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at PersonalInfo.connect(PersonalInfo.java:52)
at PersonalInfo.main(PersonalInfo.java:80)
Process completed.