太原维信科技——awt实现登陆界面+数据库
0
学java,零基础不怕,不只要理论,更要实践+项目,
<ahref=http://www.bjweixin.com>太原维信科技提供 </a>
为您讲解理论+项目 :
awt实现登陆界面+数据库
1.public class AwtTest extends Frame implements ActionListener{
MenuItem menuItem1 = null;
Menu menu3 = null;
Button b = null;
TextField text = null; 太原维信科技 http://www.bjweixin.com
public AwtTest(){
this.setLayout(null);
this.setBounds(0, 0,500,500);
this.setResizable(false);
MenuBar menuBar = new MenuBar();
Menu menu1= new Menu("学员管理");
Menu menu2 = new Menu("班级管理");
menuItem1 = new MenuItem("添加学员");
MenuItem menuItem2 = new MenuItem("删除学员");
MenuItem menuItem3 = new MenuItem("添加班级");
MenuItem menuItem4 = new MenuItem("删除班级");
this.setMenuBar(menuBar);
menuBar.add(menu1);
menuBar.add(menu2);
menu1.add(menuItem1);
menu1.add(menuItem2);
menu2.add(menuItem3);
menu2.add(menuItem4);
this.setVisible(true);
menuItem1.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setLayout(new FlowLayout());
Label name = new Label("用户名");
text = new TextField("",40);
b = new Button("登陆");
b.addActionListener(this);
add(name);
add(text);
add(b);太原维信科技 http://www.bjweixin.com
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==menuItem1){
System.out.println("----");
new AddStudent();
this.dispose();
}else if(e.getSource()==b){
String name = text.getText();
if(doLogin(name)){
new AddStudent();
this.dispose();
}else{
String message = "用户名不存在";太原维信科技
http://www.bjweixin.com
JOptionPane.showMessageDialog(this,message,"警
告",JOptionPane.WARNING_MESSAGE);
}
}
}
//1模拟数据
public boolean doLogin(String name){
//链接数据库 太原维信科技 http://www.bjweixin.com
if(name=="123"||name.equals("123")){
return true;
}else{
return false;
}
}
//2连接数据库 太原维信科技 http://www.bjweixin.com
public boolean doLogin(String name){
Connection con = null;
PreparedStatement pstmt = null;
int rs = 0;
try {
Class.forName("sun.jdbc.odbj.JdbcOdbcDriver");
con = DriverManager.getConnection
("jdbc:odbc:datasource","sa","");
String sql = "insert into table values(?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
rs = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//链接数据库
if(rs!=0){
return true;
}else{
return false;
}
}
//创建框架
public static void main(String[] args) {
AwtTest frame = new AwtTest();
}
}
public class AddStudent extends Frame{
public AddStudent(){
this.setLayout(null);
this.setBounds(20, 200,300,300);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); 太原维信科技 http://www.bjweixin.com
}
});
}
}
<a href=http://www.bjweixin.com>太原维信科技提供 </a> ,零基础学
java,嵌入式。5年经验,保您就业无忧。