📄 enrolframe.java~6~
字号:
package simpleusers;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class EnrolFrame extends JFrame { Connection con = null; Statement stmt = null; ResultSet rs = null; JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JTextField jT1 = new JTextField(); JPasswordField jP1 = new JPasswordField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); public EnrolFrame() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { jLabel1.setFont(new java.awt.Font("Dialog", 0, 16)); jLabel1.setText("用户名"); jLabel1.setBounds(new Rectangle(63, 46, 61, 30)); this.setTitle("用户管理系统-用户登录"); this.getContentPane().setLayout(null); jLabel2.setBounds(new Rectangle(63, 102, 61, 30)); jLabel2.setText("密 码"); jLabel2.setFont(new java.awt.Font("Dialog", 0, 16)); jT1.setFont(new java.awt.Font("Dialog", 0, 16)); jT1.setText(""); jT1.setBounds(new Rectangle(137, 46, 131, 37)); jP1.setText(""); jP1.setBounds(new Rectangle(137, 102, 131, 37)); jButton1.setBounds(new Rectangle(69, 175, 69, 39)); jButton1.setFont(new java.awt.Font("Dialog", 0, 14)); jButton1.setText("确定"); jButton1.addActionListener(new EnrolFrame_jButton1_actionAdapter(this)); jButton2.setText("重置"); jButton2.setFont(new java.awt.Font("Dialog", 0, 14)); jButton2.setBounds(new Rectangle(184, 175, 69, 39)); this.getContentPane().add(jLabel1, null); this.getContentPane().add(jT1, null); this.getContentPane().add(jP1, null); this.getContentPane().add(jLabel2, null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jButton2, null); } void jButton1_actionPerformed(ActionEvent e) { String username=jT1.getText().trim(); String password=jP1.getText().trim(); String sql1="select * from userandpwd where username='"+username+"'"; String sql2="insert userandpwd(username,pwd) values('"+username+ "','"+password+"')"; String msg=null; try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); }catch(ClassNotFoundException e1){ System.out.println("加载JDBC数据库驱动程序出错"); } String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Test"; try{ con = DriverManager.getConnection(url, "sa", ""); stmt = con.createStatement(); rs = stmt.executeQuery(sql1); }catch(SQLException e2){ System.out.println("数据库连接出错"); } try{ if (rs.next()) msg="对不起,你注册的用户名称已经存在!"; else{ stmt.executeUpdate(sql2); msg="欢迎你注册用户管理系统!\n"+ "你注册的用户名称为"+username+ ".\n你注册的密码为"+password+"."; } }catch(SQLException e3 ){ e3.printStackTrace(); System.out.println("插入数据出错"); } try{ rs.close(); stmt.close(); con.close(); }catch(SQLException e4){ System.out.println("关闭对象连接出错"); } MessageFrame m1=new MessageFrame(msg); m1.setSize(340,280); this.dispose(); m1.show(); }}class EnrolFrame_jButton1_actionAdapter implements java.awt.event.ActionListener { EnrolFrame adaptee; EnrolFrame_jButton1_actionAdapter(EnrolFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -