用swing mysql 写了一个QQ登录界面,但是功能实现不了,不能注册用户和登录验证解决思路
用swing mysql 写了一个QQ登录界面,但是功能实现不了,不能注册用户和登录验证
- Java code
package com.MyQQ.test; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * @author * 2012-4-4 */ public class QQFrame extends JFrame { String str1=null; String str2=null; JLabel label1=new JLabel("用户名:"); JLabel label2=new JLabel("密 码:"); JTextField tf1=new JTextField(10); JTextField tf2=new JTextField(10); JButton bt1=new JButton("登陆"); JButton bt2=new JButton("取消"); JButton bt3=new JButton("注册"); public QQFrame() { init(); } public void init(){ this.setTitle("QQVersion 0.1"); this.setBounds(200, 200, DEFULAT_WIDTH,DEFULAT_HEIGHT); bt1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { str1=tf1.getText(); str2=tf2.getText(); new QQDB().check(str1, str2); tf1.setText(""); tf2.setText(""); } }); bt2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); bt3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { str1=tf1.getText(); str2=tf2.getText(); new QQDB().addAccount(str1, str2); tf1.setText(""); tf2.setText(""); } }); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); p1.add(label1); p1.add(tf1); p2.add(label2); p2.add(tf2); p3.add(bt1); p3.add(bt2); p3.add(bt3); this.setLayout(new GridLayout(3,1)); this.add(p1); this.add(p2); this.add(p3); this.pack(); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame frame=new QQFrame(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } private static final int DEFULAT_WIDTH=150; private static final int DEFULAT_HEIGHT=80; } package com.MyQQ.test; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * @author *2012-4-4 */ public class QQDB { Connection conn=null; PreparedStatement ps=null; ResultSet res=null; String str1=null; String str2=null; String url="jdbc:mysql://localhost:3306/pwd?user=root&password=root"; public QQDB(){ init(); } public void init(){ try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url); System.out.println("连接成功"); } catch (Exception e) { e.printStackTrace(); } } //把从文本框里面取的值传进来和数据库里面的对比 public void check(String st1,String st2){ this.str1=st1; this.str2=st2; try { String sql="select userPwd from pwd where userName=str1"; ps=conn.prepareStatement(sql); ps.execute(); res=ps.executeQuery(); if (ps.execute()) { System.out.println("有此帐户 "); if(str2.equals(res.getString("userPwd"))){ System.out.println("密码正确"); }else { System.out.println("密码不正确"); } } conn.close(); ps.close(); res.close(); } catch (Exception e) { e.printStackTrace(); } } public void addAccount(String st3,String st4){ try { String sql2="insert into pwd(userName,userPwd) values(st3,st4)"; ps=conn.prepareStatement(sql2); ps.executeUpdate(); System.out.println("注册成功"); } catch (Exception e) { e.printStackTrace(); } } }