【发布时间】:2012-06-26 06:39:13
【问题描述】:
我已经制作了一个基本的 Swing 应用程序来将数据输入 MySQL 服务器。由于某种原因没有访问驱动程序以连接到数据库。这是代码。提前感谢所有答案
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class Action extends JApplet {
public void init() {
}
public Action() {
JButton button = new JButton("Click here");
button.addActionListener(new EventHandler());
add(button);
}
}
public class EventHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/testgui";
Connection con= DriverManager.getConnection(url,"root", null);
String str = JOptionPane.showInputDialog(null,"Enter type");
String abc = JOptionPane.showInputDialog(null,"Enter number");
Statement st= con.createStatement();
st.executeUpdate("insert into tb1 values (null,'"+str+"',"+abc+")");
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
【问题讨论】:
-
您是否收到错误消息?例外?
-
另外,您可能希望在 try 块中添加 finally 子句,以关闭语句和连接。
-
@John 3136 我没有收到任何异常或消息。当我在 Eclipse 中运行它时它工作正常,但是当我在浏览器上运行它时它不会在 MySQL 服务器数据库中添加数据
标签: java swing jnlp java-web-start japplet