📄 registrations.java
字号:
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
//一个注册界面
public class Registrations implements ActionListener
{
JFrame frame;
JPanel panel;
JLabel labelFName;
JLabel labelLName;
JLabel labelAddress;
JLabel labelAccType;
JLabel labelPhone;
JLabel labelAnnualIncome;
JTextField textFName;
JTextField textLName;
JTextField textAddress;
JComboBox comboAccType;
JTextField textPhone;
JTextField textAnnualIncome;
JButton buttonAccept;
JButton btnGet;
String[] AccType={"活期","定期","信用卡"};
public Registrations()
{
panel = new JPanel();
frame = new JFrame("Customer Registration");
frame.setSize(300,360);
frame.setVisible(true);
frame.getContentPane().add(panel);
labelFName = new JLabel("名");
labelLName = new JLabel("姓");
labelAddress = new JLabel("地址");
labelAccType = new JLabel("账号类型");
labelPhone = new JLabel("电话号码");
labelAnnualIncome = new JLabel("平均年收入");//年收入
textFName = new JTextField(15);
textLName = new JTextField(15);
textAddress = new JTextField(15);
comboAccType = new JComboBox(AccType);
textPhone=new JTextField(10);
textAnnualIncome = new JTextField(10);
panel.setLayout(new GridLayout(7,2));
panel.add(labelFName);
panel.add(textFName);
panel.add(labelLName);
panel.add(textLName);
panel.add(labelAddress);
panel.add(textAddress);
panel.add(labelAccType);
panel.add(comboAccType);
panel.add(labelPhone);
panel.add(textPhone);
panel.add(labelAnnualIncome);
panel.add(textAnnualIncome);
buttonAccept = new JButton("提交");
btnGet = new JButton("取消");
panel.add(buttonAccept);
panel.add(btnGet);
frame.setVisible(true);
buttonAccept.addActionListener(this);
btnGet.addActionListener(this);
}
public static void main(String args[])
{
new Registration();
}
/*try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn =
DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Hotel;User=sa;Password=sa");
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// conn = DriverManager.getConnection("jdbc:odbc:hotel");
} catch (SQLException e) {
System.err.println(e);
} catch (ClassNotFoundException e) {
System.err.println(e);
}*/
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == buttonAccept)
{
try
{
//forName()是Class的一个静态方法,用于加载驱动器
//sun.jdbc.odbc.JdbcOdbcDriver是连接字符串
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
//与相应的数据源建立连接;使用jdbc-odbc桥驱动程序
con=DriverManager.getConnection("jdbc:odbc:MyDataSource","sa","");
//创建PreparedStatement对象,可以执行参数化查询。
PreparedStatement stat2 = con.prepareStatement("insert into Registration(cFirst_name,cLast_name,cAddress,cAccount_type,cPhone_no, mAnnual_income) values(?,?,?,?,?,?)");
//从控件获得参数值
stat2.setString(1,textFName.getText());
stat2.setString(2,textLName.getText());
stat2.setString(3,textAddress.getText());
stat2.setString(4,(String)comboAccType.getSelectedItem());
stat2.setString(5,textPhone.getText());
stat2.setFloat(6,Float.parseFloat(textAnnualIncome.getText()));
//executeUpdate()方法执行语句,往表格添加数据记录
stat2.executeUpdate();
stat2.close();
con.close();//关闭连接
//JOptionPane可以显示诸如警告消息之类的对话框
JOptionPane.showMessageDialog(frame,new String("Your details have been registered"));
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(frame,new String("Error encountered while entering data in the database: "+exception));
}
}
else if(source == btnGet)//从数据库获得数据记录,查询某员工的年薪
{
String str = JOptionPane.showInputDialog(frame,new String("请输入要查询的姓名"));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con=DriverManager.getConnection("jdbc:odbc:MyDataSource","sa","");
Statement stat2 = con.createStatement();
ResultSet rs = stat2.executeQuery("select mAnnual_income from Registration where cFirst_name='"+str+"'");
System.out.println(rs.next());//移动游标,指向第一条记录
//rs.absolute(1);
float income = rs.getFloat("mAnnual_income");
textAnnualIncome.setText(Float.toString(income));
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(frame,new String("Error encountered while entering data in the database: "+exception));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -