?? tjstu.java~70~
字號:
package testsystem;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class tjStu extends JFrame {
private ImageIcon im1 = new ImageIcon("img\\forum.gif");
public tjStu() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
biaoti.setFont(new java.awt.Font("隸書", Font.BOLD, 18));
getContentPane().setLayout(null);
jt1.setBounds(new Rectangle(123, 49, 201, 31));
this.setResizable(false);
setTitle("添加用戶");
jButton1.setBounds(new Rectangle(149, 202, 108, 39));
jButton1.setIcon(im1);
jButton1.setText("添加");
jButton1.addActionListener(new tjStu_jButton1_actionAdapter(this));
jr2.setText("學生");
jr2.setBounds(new Rectangle(268, 150, 59, 35));
jr1.setText("教師");
jr1.setBounds(new Rectangle(122, 151, 76, 32));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("用戶名:");
jLabel1.setBounds(new Rectangle(27, 44, 77, 35));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("密 碼:");
jLabel2.setBounds(new Rectangle(27, 95, 73, 34));
jLabel3.setText("身 份:");
jLabel3.setBounds(new Rectangle(41, 146, 59, 40));
biaoti.setHorizontalAlignment(SwingConstants.CENTER);
biaoti.setText("添 加 用 戶");
biaoti.setBounds(new Rectangle(116, 9, 181, 33));
jp1.setBounds(new Rectangle(123, 95, 200, 30));
this.getContentPane().add(jt1);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jr1);
this.getContentPane().add(jr2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jButton1);
this.getContentPane().add(biaoti);
this.getContentPane().add(jp1);
buttonGroup1.add(jr1);
buttonGroup1.add(jr2);
}
public static void main(String[] args) {
tjStu tjstu = new tjStu();
}
JTextField jt1 = new JTextField();
JRadioButton jr1 = new JRadioButton();
JRadioButton jr2 = new JRadioButton();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel biaoti = new JLabel();
JPasswordField jp1 = new JPasswordField();
ButtonGroup buttonGroup1 = new ButtonGroup();
public void jButton1_actionPerformed(ActionEvent e) {
String Name = jt1.getText().trim();
String Psw = new String(jp1.getPassword()).trim();
Connection con =null;
Statement s1 = null;
Statement s2 = null;
Statement s3 = null;
ResultSet rs1 = null;
boolean teacher = jr1.isSelected();
boolean student = jr2.isSelected();
if((Name.length()!=0&&Psw.length()!=0)&&(teacher||student))
{
try
{
con=DBcon.link();
s1=con.createStatement();
rs1=s1.executeQuery("select * from Login where Name='"+Name+"'");
if(!rs1.next())
{
if(teacher)
{
try
{
con=DBcon.link();
s2=con.createStatement();
s2.executeUpdate("insert into Login values('"+Name+"','"+Psw+"','教師')");
JOptionPane.showMessageDialog(null,"教師用戶添加成功!");
int p=JOptionPane.showConfirmDialog(null,"您是否還要添加?","提示",JOptionPane.YES_NO_OPTION);
if(p!=0)
{
this.setVisible(false);
}
else
{
jt1.setText("");
jp1.setText("");
jt1.requestFocus();
return;
}
}
catch(SQLException ce)
{
ce.getMessage();
}
}
if(student)
{
try
{
con=DBcon.link();
s3=con.createStatement();
s3.executeUpdate("insert into Login values('"+Name+"','"+Psw+"','學生')");
JOptionPane.showMessageDialog(null,"學生用戶添加成功!");
int p=JOptionPane.showConfirmDialog(null,"您是否還要添加?","提示",JOptionPane.YES_NO_OPTION);
if(p!=0)
{
this.setVisible(false);
}
else
{
jt1.setText("");
jp1.setText("");
jt1.requestFocus();
return;
}
}
catch(SQLException ct)
{
ct.getMessage();
}
}
}
else
{
JOptionPane.showMessageDialog(null,"用戶已經存在,請重新添加!","提示",JOptionPane.ERROR_MESSAGE);
jt1.setText("");
jp1.setText("");
jt1.requestFocus();
}
}
catch(Exception co)
{
co.getMessage();
}
}
else
{
JOptionPane.showMessageDialog(null,"信息未填完全,無法添加!");
}
}
}
class tjStu_jButton1_actionAdapter implements ActionListener {
private tjStu adaptee;
tjStu_jButton1_actionAdapter(tjStu adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -