客户端登录java代码 客户端登录java代码怎么写( 五 )


sex=textField_4.getText();
if(e.getActionCommand().equals("追加"))
{
query="insert into students values("+num+","+"'"+name+"',"+age1+",'"+sex+"');";
count=1;
}
else if(e.getActionCommand().equals("查询"))
{
query="select * from students where XSB="+num+";";
count=2;
}
else if(e.getActionCommand().equals("删除"))
{
query="delete from students where XSB="+num+" and name="+"'"+name+"'";
count=3;
}
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
【客户端登录java代码 客户端登录java代码怎么写】String connectDB="jdbc:odbc:Students";
String query1=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
if(count==1)
{
query1="select * from students where XSB="+num+";";
rs=stmt.executeQuery(query1);
if(rs.next())
textField_3.setText("已经由此记录,不能追加客户端登录java代码!");
else
{
stmt.executeUpdate(query);
textField_3.setText("已经追加完成!");
}
}
else if(2==count)
{
stmt.executeQuery(query);
rs=stmt.executeQuery(query);
if(rs.next())
{
textField_3.setText("已查找到此记录!");
}
else
{
textField_3.setText("没有此记录 , 可以追加!");
}
}
else if(3==count)
{
query1="select * from students where XSB="+num+" and name="+"'"+name+"'";
rs=stmt.executeQuery(query1);
if(rs.next())
{
stmt.executeUpdate(query);
textField_3.setText("已删除此记录!");
}
else
textField_3.setText("无此记录!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
//关闭资源
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (Exception e2) {
// TODO: handle exception
}
con=null;
}
}
}
}
我用Java代码编写的一个客户端界面Client.java,我在编写个登录界面Login.java后,mport java.awt.HeadlessException;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;@SuppressWarnings("serial")public class MainFrame extends JFrame {JLabel lbl1 = new JLabel("用户名:");JLabel lbl2 = new JLabel("密码:");JTextField txt = new JTextField("admin",20);JPasswordField pwd = new JPasswordField(20);JButton btn = new JButton("登录");JPanel pnl = new JPanel();private int error = 0;public MainFrame(String title) throws HeadlessException {super(title);init();}private void init() {this.setResizable(false);pwd.setEchoChar('*');pnl.add(lbl1);pnl.add(txt);pnl.add(lbl2);pnl.add(pwd);pnl.add(btn);this.getContentPane().add(pnl);btn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if ("admin".equals(new String(pwd.getPassword()))){pnl.removeAll();JLabel lbl3 = new JLabel();ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));lbl3.setIcon(icon);pnl.add(lbl3);}else{if(error3){JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次");error++;}else{JOptionPane.showMessageDialog(null,"对不起,您不是合法用户");txt.setEnabled(false);pwd.setEnabled(false);btn.setEnabled(false);}}}});}public static void main(String[] args) {MainFrame frm = new MainFrame("测试");frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frm.setBounds(100, 100, 300, 120);frm.setVisible(true);}}

推荐阅读