Results 1 to 4 of 4
Thread: Can NOT connect to Mysql
- 12-18-2009, 07:40 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
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
code
Errors CodeJava Code: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); } }); } }
Java 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.
- 12-18-2009, 08:07 PM #2
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
Is MySQL driver in your classpath?
Please don't laugh at my English... I'm trying my best! :)
- 12-18-2009, 08:14 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
Yes it is, like I said if I don't use the form code and just use the code that connect to database it works like charm.
Zed
- 12-18-2009, 08:26 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
J2EE = Servlet+Mysql connect
By initmax in forum Java ServletReplies: 2Last Post: 10-30-2009, 09:42 AM -
J2EE = Servlet+Mysql connect
By initmax in forum Java ServletReplies: 2Last Post: 10-30-2009, 07:57 AM -
connect java with mysql in rhel5
By niraj.vara in forum New To JavaReplies: 0Last Post: 07-17-2009, 09:23 AM -
Connect applet with mysql
By baltimore in forum Java AppletsReplies: 1Last Post: 08-07-2007, 06:39 AM -
Connect applet with mysql
By Felissa in forum Java AppletsReplies: 3Last Post: 07-05-2007, 06:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks