Aruz Posted November 17, 2008 Share Posted November 17, 2008 I try to make a mysql database program in Eclipse. But I can't connect. What is the problem? package pack; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane; public class SwingMysql extends JPanel implements ActionListener { //*************** JButton button; JTextField driver,url,user,pass,database; JTextPane status; //*************** public SwingMysql(){ url=new JTextField(20); url.setText("jdbc:mysql://localhost:3306/ss3bfd"); database=new JTextField(20); database.setText("SELECT display_name, user_email,user_nicename FROM wp_users"); user=new JTextField(5); user.setText("root"); pass=new JTextField(5); pass.setText("1234"); status=new JTextPane(); status.setText("\r ready \r"); button=new JButton("Button"); button.setActionCommand("Command1"); button.addActionListener(this); add(url); add(database); add(user); add(pass); add(button); add(status); } //*************** private static void makewin(){ JFrame.setDefaultLookAndFeelDecorated(true); JFrame myframe =new JFrame("Swing Mysql"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SwingMysql floor=new SwingMysql(); floor.setOpaque(true); myframe.setContentPane(floor); myframe.pack(); myframe.setSize(300, 400); myframe.setLocation(300, 300); myframe.setVisible(true); } //*************** public void actionPerformed(ActionEvent e){ if("Command1".equals(e.getActionCommand())){ try { status.setText("\r start "); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn=DriverManager.getConnection(url.getText(),user.getText(),pass.getText()); Statement stat=conn.createStatement(); status.setText("connected"); ResultSet re=stat.executeQuery(database.getText()); while (re.next()){ String x=re.getString("display_name"); } status.setText("congratulations"); conn.close(); } catch(Exception e1){ status.setText("Error: "+ e1.getStackTrace()); } } } //*************** public static void main(String[] args) { // TODO Auto-generated method stub makewin(); } } Thanks for your time. Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/ Share on other sites More sharing options...
0 Antaris Veteran Posted November 17, 2008 Veteran Share Posted November 17, 2008 Whats the error? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590138060 Share on other sites More sharing options...
0 Aruz Posted November 17, 2008 Author Share Posted November 17, 2008 I can't connect. I get this error. Error: [Ljava.lang.StackTraceElement;@193f6e2 Why I can't connect? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590138068 Share on other sites More sharing options...
0 Antaris Veteran Posted November 17, 2008 Veteran Share Posted November 17, 2008 Is that the whole error message? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590138090 Share on other sites More sharing options...
0 Aruz Posted November 17, 2008 Author Share Posted November 17, 2008 Error: [Ljava.lang.StackTraceElement;@18352d8 Error: [Ljava.lang.StackTraceElement;@19a029e Error: [Ljava.lang.StackTraceElement;@17a4989 And some thing like these whenever I try. I using Vista and EasyPHP. I try this for localhost. Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590138114 Share on other sites More sharing options...
0 JamesCherrill Posted November 17, 2008 Share Posted November 17, 2008 You should get more info by putting an e1. printStackTrace(); in the catch Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590138210 Share on other sites More sharing options...
0 Aruz Posted November 17, 2008 Author Share Posted November 17, 2008 I try to run also java2s samples but I can't connect. I send a copy one of my friends. And he can't connect too. What do I wrong? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590140506 Share on other sites More sharing options...
0 JamesCherrill Posted November 18, 2008 Share Posted November 18, 2008 I try to run also java2s samples but I can't connect. I send a copy one of my friends. And he can't connect too. What do I wrong? put an e1. printStackTrace(); in your catch to see exactly what the error is and where it's happening. Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590142806 Share on other sites More sharing options...
0 Aruz Posted November 18, 2008 Author Share Posted November 18, 2008 I replace status.setText("Error: "+ e1.getStackTrace()); with e1.getStackTrace(); java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at pack.SwingMysql.actionPerformed(SwingMysql.java:61) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Thanks.. Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590146000 Share on other sites More sharing options...
0 JamesCherrill Posted November 18, 2008 Share Posted November 18, 2008 ClassNotFoundException: com.mysql.jdbc.Driver Looks like the driver isn't correctly installed or it's not in the Java classpath. Double check the installation instructions for the mysql java driver? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590146110 Share on other sites More sharing options...
0 Aruz Posted November 19, 2008 Author Share Posted November 19, 2008 Thanks a lot JamesCherrill ... I installed the driver. mysql-connector-java.jar. Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590149382 Share on other sites More sharing options...
0 JamesCherrill Posted November 19, 2008 Share Posted November 19, 2008 I installed the driver. mysql-connector-java.jar. How and where did you install it? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590149920 Share on other sites More sharing options...
0 Aruz Posted November 20, 2008 Author Share Posted November 20, 2008 I have been downloaded it from http://www.mysql.com/products/connector/j/ I unzip the mysql-connector-java-5.1.7.zip and copy to my project's directory the mysql-connector-java-5.1.7-bin.jar file. And I renamed it such as mysql-connector-java.jar. And I imported this file in the project and run. Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590153992 Share on other sites More sharing options...
0 JamesCherrill Posted November 20, 2008 Share Posted November 20, 2008 Sorry, bit confused here - is it working now? Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590154148 Share on other sites More sharing options...
0 Aruz Posted November 20, 2008 Author Share Posted November 20, 2008 Yes it is working now. The driver was not in the Java classpath. When I moved to the project directory it run. Thanks for your help. I am learning. :) Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590157732 Share on other sites More sharing options...
0 JamesCherrill Posted November 21, 2008 Share Posted November 21, 2008 OK Aruz. Glad to help. James Link to comment https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/#findComment-590159550 Share on other sites More sharing options...
Question
Aruz
I try to make a mysql database program in Eclipse. But I can't connect. What is the problem?
package pack; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane; public class SwingMysql extends JPanel implements ActionListener { //*************** JButton button; JTextField driver,url,user,pass,database; JTextPane status; //*************** public SwingMysql(){ url=new JTextField(20); url.setText("jdbc:mysql://localhost:3306/ss3bfd"); database=new JTextField(20); database.setText("SELECT display_name, user_email,user_nicename FROM wp_users"); user=new JTextField(5); user.setText("root"); pass=new JTextField(5); pass.setText("1234"); status=new JTextPane(); status.setText("\r ready \r"); button=new JButton("Button"); button.setActionCommand("Command1"); button.addActionListener(this); add(url); add(database); add(user); add(pass); add(button); add(status); } //*************** private static void makewin(){ JFrame.setDefaultLookAndFeelDecorated(true); JFrame myframe =new JFrame("Swing Mysql"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SwingMysql floor=new SwingMysql(); floor.setOpaque(true); myframe.setContentPane(floor); myframe.pack(); myframe.setSize(300, 400); myframe.setLocation(300, 300); myframe.setVisible(true); } //*************** public void actionPerformed(ActionEvent e){ if("Command1".equals(e.getActionCommand())){ try { status.setText("\r start "); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn=DriverManager.getConnection(url.getText(),user.getText(),pass.getText()); Statement stat=conn.createStatement(); status.setText("connected"); ResultSet re=stat.executeQuery(database.getText()); while (re.next()){ String x=re.getString("display_name"); } status.setText("congratulations"); conn.close(); } catch(Exception e1){ status.setText("Error: "+ e1.getStackTrace()); } } } //*************** public static void main(String[] args) { // TODO Auto-generated method stub makewin(); } }Thanks for your time.
Link to comment
https://www.neowin.net/forum/topic/697864-swing-mysql-connection-problem/Share on other sites
15 answers to this question
Recommended Posts