【发布时间】:2014-10-13 15:13:48
【问题描述】:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class RegistrationForm extends JFrame {
private JPanel panel;
private JLabel user,pass,id;
private JTextField user1,pass1,id1;
private JButton save,signIn;
public Connection con;
public Statement stm;
public PreparedStatement pstm;
public RegistrationForm(){
setTitle("Register Account");
setLayout(new FlowLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\RenRen\\Documents\\NetBeansProjects\\JavaConnector\\src\\images\\reg.jpg")));
JLabel sign = new JLabel("Sign Up:");
sign.setBounds(100,95,80,30);
sign.setFont(new Font("Login",Font.ITALIC,20));
id= new JLabel("User ID");
user = new JLabel("Username:");
pass = new JLabel("Password:");
JLabel acc = new JLabel("Already have an Account?");
id.setBounds(300,95,80,30);
user.setBounds(130,140,80,20);
pass.setBounds(250,140,80,20);
acc.setBounds(250,230,150,20);
id1 = new JTextField(5);
user1 = new JTextField(10);
pass1 = new JPasswordField(10);
id1.setBounds(350,100,80,20);
user1.setBounds(130,160,110,20);
pass1.setBounds(250,160,110,20);
save = new JButton("Save");
signIn = new JButton("Sign In");
save.setBounds(150,200,90,20);
signIn.setBounds(250,200,90,20);
add(sign);
add(id);
add(id1);
add(user);
add(user1);
add(pass);
add(pass1);
add(save);
add(signIn);
add(acc);
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","");
stm=con.createStatement();
}
catch(Exception e)
{ JOptionPane.showMessageDialog(null, e);
}
ActionListener action = new ActionHandler();
save.addActionListener(action);
signIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(null, "Thank You!");
dispose();
LoginMain LM = new LoginMain();
LM.run();
}});
}
private class ActionHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == save){
String sql="Insert into logform(username,password)values(?,?)";
try{
pstm = con.prepareStatement(sql);
pstm.setString(1, user1.getText());
pstm.setString(2, pass1.getText());
pstm.execute();
JOptionPane.showMessageDialog(null, "Saved!");
}catch(Exception se){
JOptionPane.showMessageDialog(null, "Save Failed");
}
}
}
}
public void run(){
setSize(500,300);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
RegistrationForm rf = new RegistrationForm();
rf.run();
}
}
我想将 MySQL 数据库中的数据显示到 JTextField。例如,MySQL 数据库中 UserID 的数据为 1,我想在 JTextField 中显示该数据,他们无法在 JTextField 中编辑文本。
谁能帮帮我?
【问题讨论】:
-
从一些教程开始,如果您发现一些问题,请编写您的要求,然后使用适当的堆栈跟踪发布您的代码
标签: java mysql jtextfield