• 0

Swing mysql connection problem


Question

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

  • 0

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.

  • 0

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..

  • 0

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.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.