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

    • Brave Browser 1.91.172 is out.
    • Any Video Converter Free 9.2.3 by Razvan Serea Any Video Converter is an All-in-One video converting tool with an easy-to-use graphical interface, fast converting speed and excellent video quality. Any Video Converter supports all popular video formats and converts your videos to different video formats including MP4, MOV, MKV, M2TS, M4V, MPEG, AVI, WMV, ASF, OGV, WEBM, and more. It supports converting videos to customized percent (50%, 100%, 200%, and more) or resolution (480p, 720p, 1080p, 4K, and more); It supports encoding videos into x264, x265, h263p, xvid, mpeg, wmv, and more. Any Video Converter Free key features: Compatible with Windows 11/10/8.1/8/7 (32-64bit) User interface are available in 14 languages Convert all kinds of video formats including high-definition videos Extract audio from any videos and save as MP3/WMA for your mp3 player Take snapshot from any videos and build your own picture collection Support high-definition for both input and output Batch add videos from hard drive and batch convert Customize output parameters completely as you like Manage your output videos files by group or output profile Merge several video files into a single and long one Clip a video into segments Free Audio Filter: Adjust audio volume and add audio effects Crop frame size to remove black bars and retain what you want only Adjust the brightness, contrast, saturation Rotate or flip or add noise/sharpen effects Produce output video with subtitles of your own dialogue and much, much more... Any Video Converter Free 9.2.3 changelog: Fixed video download engine auto-update failures. Added custom speed control support in the speed change tool. Added support for downloading YouTube AI-generated subtitles. Added support for preserving original audio stream in the format convert tool (e.g., Dolby Atmos, DTS:X). Fixed other bugs and improved overall performance. Download: Any Video Converter Free 9.2.3 | 7.6 MB (Freeware) View: Any Video Converter Free Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Not sure what country you’re in but in many countries you can absolutely jail the sellers behind businesses… in fact I’d say in most countries you can do that
    • I guess we are done since you refuse to read my comment you replied to or my other comment in another thread you were also a part of here.
    • I hate Slack, but Teams is much worse - maybe they've already optimized it, but it was a huge slog the last time I've checked about half a year ago. I miss my Skype.
  • Recent Achievements

    • Dedicated
      jordanspringer earned a badge
      Dedicated
    • Rookie
      Rimplesnort went up a rank
      Rookie
    • One Year In
      Markus94287 earned a badge
      One Year In
    • One Month Later
      Markus94287 earned a badge
      One Month Later
    • Week One Done
      Markus94287 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      505
    2. 2
      +Edouard
      175
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!