javaSe注册登录GUI

来源:互联网 发布:国际投资净头寸数据 编辑:程序博客网 时间:2024/06/06 03:02

1.关于jdbc的操作类

package com.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;public class JdbcTest {String className="com.mysql.jdbc.Driver";String url="jdbc:mysql://localhost:3306/student?useUnicode=true&&characterEncoding=utf-8";String user="root";String password="root";ResultSet set= null;//表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。 //ResultSet 对象具有指向其当前数据行的光标。最初,光标被置于第一行之前。next 方法将光标移动到下一行;//因为该方法在 ResultSet 对象没有下一行时返回 false,所以可以在 while 循环中使用它来迭代结果集。//........................................登陆.............................................public boolean login(String name,String pwd){try {Class.forName(className);Connection con = DriverManager.getConnection(url, user, password);Statement st = con.createStatement();String sql="select * from student where name='"+name+"' and pwd='"+pwd+"'";ResultSet rs = st.executeQuery(sql);if(rs.next()){//如果根据传来的值从数据库中获取到当前对象,则rs.next()返回true ,若传来的值在数据库中不存在,则返回falsereturn true;}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return false;}//..............................................注册.......................................public boolean insert(String name,String pwd){try {Class.forName(className);Connection con = DriverManager.getConnection(url, user, password);Statement st = con.createStatement();String sql="insert into student values("+null+",'"+name+"',"+"'"+ pwd+"')";int result = st.executeUpdate(sql);//提交sql语句,若数据库执行该语句成功,则会返回一个大于0的值if(result >0){   //根据数据库的返回值,判断是否成功插入System.out.println("success");return true;}else{System.out.println("error");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return false;}//..............................................删除.......................................public boolean delete(String name,String pwd){try {Class.forName(className);Connection con = DriverManager.getConnection(url, user, password);Statement st = con.createStatement();String sql="delete from student where name ='"+ name+"'";int result = st.executeUpdate(sql);//提交sql语句,若数据库执行该语句成功,则会返回一个大于0的值if(result >0){   //根据数据库的返回值,判断是否成功插入System.out.println("success");return true;}else{System.out.println("error");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return false;}//..............................................修改.......................................public boolean update(String name,String pwd){try {Class.forName(className);Connection con = DriverManager.getConnection(url, user, password);Statement st = con.createStatement();String sql="update student set pwd = '"+pwd+"'"+"where name='"+ name+"'";int result = st.executeUpdate(sql);//提交sql语句,若数据库执行该语句成功,则会返回一个大于0的值if(result >0){   //根据数据库的返回值,判断是否成功插入System.out.println("success");return true;}else{System.out.println("error");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return false;}//..............................................查询.......................................public String chax(int id){try {//注册驱动Class.forName(className);//建立连接    Connection con = DriverManager.getConnection(url, user, password);    //新建状态    Statement st= con.createStatement();//Statement 对象表示基本语句,其中将单个方法应用于某一目标和一组参数,以返回结果,    //statement 对象不需要名称空间,可以使用值本身构造。statement 对象将指定方法与其环境相关联,作为值的简单集合:目标和参数值数组。     // 提交sql    String sql ="select * from student where id ="+id;     set = st.executeQuery(sql);    while(set.next()){    System.out.println("name="+set.getString(2));    System.out.println("pwd="+set.getString(3));    String value=set.getString(2)+","+set.getString(3);    return value;    }} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally{if(set!=null){try {set.close();} catch (SQLException e) {e.printStackTrace();}}}return "无";}//..............................................select().......................................public void select(){try {//注册驱动Class.forName(className);//建立连接    Connection con = DriverManager.getConnection(url, user, password);    //新建状态    Statement st= con.createStatement();//Statement 对象表示基本语句,其中将单个方法应用于某一目标和一组参数,以返回结果,    //statement 对象不需要名称空间,可以使用值本身构造。statement 对象将指定方法与其环境相关联,作为值的简单集合:目标和参数值数组。     // 提交sql    String sql ="select * from emp";     set = st.executeQuery(sql);    while(set.next()){    System.out.println("id="+set.getString(2));    }} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally{if(set!=null){try {set.close();} catch (SQLException e) {e.printStackTrace();}}}}//........................................select1().............................................public List<Emp> select1(){List<Emp> list = new ArrayList<Emp>();try {//注册驱动Class.forName(className);//建立连接    Connection con = DriverManager.getConnection(url, user, password);    //新建状态    Statement st= con.createStatement();    // 提交sql    String sql ="select * from emp";    set = st.executeQuery(sql);        while(set.next()){    //System.out.println("id="+set.getString(2));    Emp e = new Emp();    e.setId(set.getInt(1));    e.setName(set.getString(2));    list.add(e);    }} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally{if(set!=null){try {set.close();} catch (SQLException e) {e.printStackTrace();}}}return list ; }//......................................insertEmp()...............................................public void insertEmp(String name) {      try {Class.forName(className);//注册驱动Connection con = DriverManager.getConnection(url, user, password);//建立连接Statement st = con.createStatement();//新建状态String sql="insert into emp values(null,'"+name+"')"; //sql语句//String sqlUpdate = "udpate emp set name='"+name+"' where id =14 ";int result = st.executeUpdate(sql);//提交sql语句,若数据库执行该语句成功,则会返回一个大于0的值if(result >0){   //根据数据库的返回值,判断是否成功插入System.out.println("success");}else{System.out.println("error");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}      }}

2.GUI界面

login.java

package com.gui;import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import com.jdbc.JdbcTest;@SuppressWarnings("serial")public class login extends JFrame implements ActionListener{private JPanel contentPane;private JTextField name ;private JTextField pwd ;private JTextField idz ;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {//不允许同时从该队列中指派多个事件。指派顺序与它们排队的顺序相同//invokeLater(Runnable runnable) 导致 runnable 的 run 方法在 the system EventQueue 的指派线程中被调用。//使用实现接口 Runnable 的对象创建一个线程时,启动该线程将导致在独立执行的线程中调用对象的 run 方法。 public void run() {try {login frame = new login();frame.createGUI();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public void createGUI() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置用户在此窗体上发起 "close" 时默认执行的操作。setBounds(500, 150, 450, 350);contentPane = new JPanel(); //指定容器setContentPane(contentPane);//设置 contentPane 属性contentPane.setLayout(null);JButton reset =new JButton("信息重置");//按钮定义reset.setBounds(69, 193, 94, 23);contentPane.add(reset);JButton submit =new JButton("用户登陆");submit.setBounds(169, 193, 94, 23);contentPane.add(submit);JButton zhuce =new JButton("用户注册");zhuce.setBounds(269, 193, 94, 23);contentPane.add(zhuce);JButton xiug =new JButton("修改密码");xiug.setBounds(69, 233, 94, 23);contentPane.add(xiug);JButton shanc =new JButton("删除账号");shanc.setBounds(169, 233, 94, 23);contentPane.add(shanc);JButton chax =new JButton("查询id号");chax.setBounds(269, 233, 94, 23);contentPane.add(chax);reset.addActionListener(this);  //按钮事件submit.addActionListener(this);zhuce.addActionListener(this);xiug.addActionListener(this);shanc.addActionListener(this);chax.addActionListener(this);JLabel namem = new JLabel("账号");namem.setBounds(69, 57, 30, 15);//setBounds(int x,int y,int width,int height)contentPane.add(namem);name = new JTextField();name.setBounds(115, 54, 242, 21);contentPane.add(name);name.setColumns(10);// 设置此 TextField 中的列数,然后验证布局。JLabel pwdm = new JLabel("密码");pwdm.setBounds(69, 107, 30, 15);//setBounds(int x,int y,int width,int height)contentPane.add(pwdm);pwd = new JTextField();pwd.setBounds(115, 104, 242, 21);contentPane.add(pwd);pwd.setColumns(10);// 设置此 TextField 中的列数,然后验证布局。JLabel idm = new JLabel("id");idm.setBounds(69, 157, 30, 15);//setBounds(int x,int y,int width,int height)contentPane.add(idm);idz = new JTextField();idz.setBounds(115, 154, 242, 21);contentPane.add(idz);pwd.setColumns(10);// 设置此 TextField 中的列数,然后验证布局。}public void actionPerformed(ActionEvent e) {String result = e.getActionCommand();if(result.equals("信息重置")){name.getText(); //先获取值pwd.getText();name.setText("");//然后在置空pwd.setText("");}else if(result.equals("用户登陆")){String nameSql= name.getText();String pwdSql = pwd.getText();boolean res = new JdbcTest().login(nameSql, pwdSql);if(res){System.out.println("login success");}else{System.out.println("login error");}}else if(result.equals("用户注册")){String nameSql= name.getText();String pwdSql = pwd.getText();//判断数据库中是否有该数据boolean res2 = new JdbcTest().insert(nameSql, pwdSql);if(res2){System.out.println("注册成功");}else{System.out.println("注册失败");}}else if(result.equals("修改密码")){String nameSql= name.getText();String pwdSql = pwd.getText();boolean res2 = new JdbcTest().update(nameSql, pwdSql);if(res2){System.out.println("修改成功");}else{System.out.println("修改失败");}}else if(result.equals("删除账号")){String nameSql= name.getText();String pwdSql = pwd.getText();boolean res2 = new JdbcTest().delete(nameSql, pwdSql);if(res2){System.out.println("删除成功");}else{System.out.println("删除失败");}}else if(result.equals("查询id号")){String idSql = idz.getText();String res2 = new JdbcTest().chax(Integer.parseInt(idSql));System.out.println("res="+res2);String a[] = res2.split(",");System.out.println("查询成功");name.getText(); //先获取值pwd.getText();name.setText(a[0]);//然后在置空pwd.setText(a[1]);}}}