java简单界面的注册功能——数据库实现
有关数据库方面的操作本次不打算讲了,不懂的可以看本人的上一次的技术博客。今天给大家带来的是本人自己思索过后对数据库的基本使用。只是简单的记录了自己的学习过程和经历,好了,言归正传。我这次是做了一个简单的界面:
package ctong; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTextField; /** * 1.增加数据-insert into game(name,type) values(?,?) * public void dd2Table(String name,String type); * public void add2Table(String name,String type,int id); * 2.删除数据-delete from game where id = ? * public void DelData(int id) * 3.查找数据-select * from game * public void SelectTables() * 4.修改数据-update game set name=?,type=? where id=? * public void upData(String name,String type,int id) * * * * @author ctong * */ public class Mysql { private JTextField field1,field2; private ArrayList<String> arrayList=new ArrayList<String>(); /* 数据库加载固定属性 */ //mySql 中驱动类的名字就是其驱动包中的com.mysql.jdbc.Driver.class 类。 String driver="com.mysql.jdbc.Driver"; Connection con; //jdbc:mysql://ip 地址: 端口号/数据库名字 String url="jdbc:mysql://localhost:3306/ctong"; String user="root"; //连接上数据库mysql public void connection2MYSQL() { try { Class.forName(driver); //第三个属性是密码 con=DriverManager.getConnection(url,user,""); if(!con.isClosed()) System.out.println("连接成功"); } catch (Exception e) { e.printStackTrace(); } } //在表末增加一栏数据 public void add2Table(String zhanghao,String mima) { try { //预处理命令添加 PreparedStatement sql; sql = con.prepareStatement("insert into QQ(zhanghao,mima) values(?,?)"); sql.setString(1,zhanghao); sql.setString(2,mima); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //增加一栏数据-----指定id public void add2Table(String zhanghao,String mima,int id) { try { //预处理命令添加 PreparedStatement sql; sql = con.prepareStatement("insert into QQ(zhanghao,mima,id) values(?,?,?)"); sql.setString(1,zhanghao); sql.setString(2,mima); sql.setInt(3, id); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //查找表中所有数据 public void SelectTables(){ boolean flag = false; try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from QQ"; ResultSet rs = statement.executeQuery(sql); String zhanghao = ""; String mima = ""; int id; while (rs.next()) { zhanghao = rs.getString("zhanghao"); mima = rs.getString("mima"); id=rs.getInt("id"); System.out.println("zhanghao =" + zhanghao + " mima=" + mima+" id="+id); if(zhanghao.equals(field1.getText())&&mima.equals(field2.getText())){ flag=true; JOptionPane.showMessageDialog(null, "成功登陆"); } } if(!flag) JOptionPane.showMessageDialog(null, "登陆失败"); rs.close(); } catch (Exception e) { e.printStackTrace(); } } public boolean SelectTables2(){ boolean flag = false; try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from QQ"; ResultSet rs = statement.executeQuery(sql); String zhanghao = ""; while (rs.next()) { zhanghao = rs.getString("zhanghao"); if(zhanghao.equals(field1.getText())){ flag=true; JOptionPane.showMessageDialog(null, "注册失败"); return false; } } if(!flag) JOptionPane.showMessageDialog(null, "注册成功"); rs.close(); } catch (Exception e) { e.printStackTrace(); } return true; } //删除一行数据 public void DelData(int id) { try { PreparedStatement sql; sql = con.prepareStatement("delete from QQ where id = ?"); sql.setInt(1,id); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public void DeleteData(String zhanghao) { try { PreparedStatement sql; sql = con.prepareStatement("delete from QQ where zhanghao = ?"); sql.setString(1,zhanghao); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //修改一栏数据 public void upData(String zhanghao,String mima,int id) { try { PreparedStatement sql; sql = con.prepareStatement("update QQ set zhanghao=?,mima=? where id=?"); sql.setString(1,zhanghao); sql.setString(2,mima); sql.setInt(3, id); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //数据库的关闭 public void close1(){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } //数据库内容初始化 public Object[] showR(){ try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from QQ"; ResultSet rs = statement.executeQuery(sql); String zhanghao = ""; while (rs.next()) { zhanghao = rs.getString("zhanghao"); arrayList.add(zhanghao); } rs.close(); } catch (Exception e) { e.printStackTrace(); } return arrayList.toArray(); } //界面 public void init(){ final JFrame frame = new JFrame("我的界面注册功能"); frame.setSize(500, 500); frame.setDefaultCloseOperation(0); frame.setLocationRelativeTo(null); frame.setResizable(false); frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); frame.setLayout(new FlowLayout()); JLabel label1 = new JLabel("账号"); JLabel label2 = new JLabel("密码"); field1 = new JTextField(10); field2 = new JTextField(10); JButton button1 = new JButton("登陆"); JButton button2 = new JButton("注册"); final JList list = new JList(); list.setForeground(Color.BLUE); list.setCursor(new Cursor(12)); list.setBorder(BorderFactory.createTitledBorder("已有的账号")); list.setListData(showR()); list.setToolTipText("双击删除账号!"); list.setFont(new Font(null, Font.BOLD, 17)); JScrollPane jsp = new JScrollPane(list); jsp.setPreferredSize(new Dimension(400,400)); frame.add(label1); frame.add(field1); frame.add(label2); frame.add(field2); frame.add(button1); frame.add(button2); frame.add(jsp); frame.setVisible(true); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(!field1.getText().isEmpty()){ SelectTables(); }else JOptionPane.showMessageDialog(null, "账号密码不能为空"); } }); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(!field1.getText().isEmpty()&&!field2.getText().isEmpty()){ if(SelectTables2()){ add2Table(field1.getText(), field2.getText()); arrayList.add(field1.getText()); list.setListData(arrayList.toArray()); } }else JOptionPane.showMessageDialog(null, "账号密码不能为空"); } }); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if(e.getClickCount()==2){ JOptionPane.showMessageDialog(null, "账号:"+list.getSelectedValue()+"已删除"); DeleteData((String) list.getSelectedValue()); arrayList.remove(list.getSelectedIndex()); list.setListData(showR()); } } }); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ int result=JOptionPane.showConfirmDialog(null, "你确定要退出?", "Information", JOptionPane.YES_NO_OPTION); if(result==JOptionPane.YES_NO_OPTION){ //这里用这个比较合适,因为这样是直接退出程序,而dispose()只关闭窗体,而程序还没结束。 close1(); System.exit(0); } } }); } //主函数 public static void main(String[] args) { Mysql mysql = new Mysql(); mysql.connection2MYSQL(); mysql.init(); } }