• 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

    • When will the Photos app be updated to remember the window size and position when reopened? They addressed this issue in a 2024 version of the app (though I can't recall the build number). Unfortunately, after that specific version, the problem persists! Please prioritise this fix in your K2 schedule. Additionally, the Snipping Tool has lost the ability to capture the Windows Taskbar starting from the 2024 version!
    • Same, never saw it on Android or iOS. Guess only some people got it *shrugs*
    • Anthropic pulls Fable 5 and Mythos 5 after US export control order by Pradeep Viswanathan In April this year, Anthropic launched the Claude Mythos Preview frontier model with state-of-the-art cyber and coding capabilities for a select set of companies around the world. After preparing appropriate guardrails, early this week, Anthropic launched Claude Fable 5 and Mythos 5, its most capable AI models. Claude Fable 5 is for general users and comes with strict safeguards, while Mythos 5 is designed with fewer safeguards for cybersecurity and biology use cases. Today, Anthropic abruptly suspended access to its Fable 5 and Mythos 5 AI models for all customers after receiving an export control directive from the US government. The company received the directive from the government today at 5:21 p.m. ET, and the received letter did not provide any details regarding the national security concern. Anthropic understands that the government became aware of a method to bypass, or “jailbreak,” Fable 5, which might be the reason behind the directive. The order was issued under national security authorities and requires the company to suspend all access to Fable 5 and Mythos 5 by any foreign national, whether they are inside or outside the United States. The restriction also applies to foreign national employees working at Anthropic. As a result, the company has disabled both models for all customers to ensure compliance. Access to previous Anthropic models like Opus and Sonnet is not affected by this government order. The company highlighted that it had developed strong safeguards to reduce the possibility that Fable is misused for tasks related to cybersecurity. In fact, many developers are complaining that the safeguards are going overboard. Additionally, the company worked with the US government, the UK AISI, multiple private third-party organizations, and internal teams to red-team Fable’s safeguards for thousands of hours. Finally, Anthropic noted that no testers have yet been able to find a universal jailbreak on Fable 5. As expected, Anthropic disagrees that a narrow potential jailbreak should lead to the recall of a commercial model used by hundreds of millions of people. It warned that applying this standard across the AI industry could effectively halt new frontier model deployments. Anthropic concluded by mentioning that it is working to restore access to Fable 5 and Mythos 5 as soon as possible and plans to share more details within the next 24 hours.
    • Brave Browser 1.91.172 is out.
  • Recent Achievements

    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • 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
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      176
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      92
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!