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

Link to comment
Share on other sites

  • 0
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
Share on other sites

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

Link to comment
Share on other sites

  • 0

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
Share on other sites

  • 0

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
Share on other sites

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

    • No registered users viewing this page.