package  注册信息;     
import  java. sql. Connection; 
import  java. sql. DriverManager; 
import  java. sql. Statement;   public  class  conn_db {   Connection con;   String url =  null;   Statement stmt;   public  void  connection ( )  throws  ClassNotFoundException{   url =  "jdbc:mysql://localhost:3306/wang?user=root&password=wsq&useUnicode=true&characterEncoding=UTF8" ;   
try  { Class. forName ( "com.mysql.jdbc.Driver" ) ; con =  DriverManager. getConnection ( url) ; System. out. println ( "连接成功" ) ;   stmt =  con. createStatement ( ) ; } catch  ( Exception  e)  { e. printStackTrace ( ) ; } }   public  static  void  main ( String[ ]  args)  throws  ClassNotFoundException {   conn_db conn =  new  conn_db ( ) ;   conn. connection ( ) ;   }   
}   package  注册信息; import  java. awt. event. ActionEvent; 
import  java. awt. event. ActionListener; 
import  java. sql. Connection; 
import  java. sql. ResultSet; 
import  java. sql. Statement; import  javax. swing. *; public  class  login_db  extends  conn_db  implements  ActionListener  { JTextField accT, nameT; JButton okB, registB; register re; ResultSet rs; public  void  setaccountT ( JTextField a) { accT =  a; } public  void  setnameT ( JTextField n) { nameT =  n; } public  void  setButton ( JButton b1, JButton b2) { okB =  b1; registB =  b2; } public  void  actionPerformed ( ActionEvent e) { if ( e. getSource ( )  ==  okB) { if ( accT. getText ( ) . equals ( "" ) ) 			JOptionPane. showMessageDialog ( null,  "请填写账号!" ) ; else  if ( nameT. getText ( ) . equals ( "" ) ) JOptionPane. showMessageDialog ( null,  "请输入密码" ) ; else { String accountT =  accT. getText ( ) ; String namesT =  nameT. getText ( ) ; try  { connection ( ) ;  		boolean  com =  compareWithSql ( accountT, namesT) ; if ( com) JOptionPane. showMessageDialog ( null,  "登录成功" ) ; else { JOptionPane. showMessageDialog ( null,  "账号或密码不正确,请重新输入" ) ; accT. setText ( "" ) ; nameT. setText ( "" ) ; } }  catch  ( Exception  e1)  { e1. printStackTrace ( ) ; } } } else  if ( e. getSource ( )  ==  registB) { new  JFrame ( ) . dispose ( ) ; re =  new  register ( ) ; } } boolean  compareWithSql ( String accountT, String namesT)  throws  Exception{ String sql; 		Connection con =  super . con; Statement stmt =  con. createStatement ( ) ; sql =  "select * from my" ; 
rs =  stmt. executeQuery ( sql) ; while ( rs. next ( ) ) { 				String acc =  rs. getString ( 1 ) ; String names =  rs. getString ( 2 ) ; if ( acc. equals ( accountT)  &&  names. equals ( namesT) ) { return  true ; } 
} 
return  false ; } } 
package  注册信息; import  java. awt. FlowLayout; 
import  java. awt. event. ActionListener; import  javax. swing. JFrame; 
import  javax. swing. *; public  class  login  extends  JFrame { JTextField accountT, nameT; JButton okB, registB; Box baseB1, baseB2, box1, box2, box3; 		login_db log; login ( ) { init ( ) ; } void  init ( ) { log =  new  login_db ( ) ; accountT =  new  JTextField ( 10 ) ; nameT =  new  JTextField ( 20 ) ; okB =  new  JButton ( "登录" ) ; registB =  new  JButton ( "注册" ) ; box1 =  Box. createVerticalBox ( ) ; box1. add ( new  JLabel ( "账号:" ) ) ; box1. add ( Box. createVerticalStrut ( 8 ) ) ; box1. add ( new  JLabel ( "密码" ) ) ; box2 =  Box. createVerticalBox ( ) ; box2. add ( accountT) ; box2. add ( Box. createVerticalStrut ( 8 ) ) ; box2. add ( nameT) ; box3 =  Box. createHorizontalBox ( ) ; box3. add ( okB) ; box3. add ( Box. createHorizontalStrut ( 20 ) ) ; box3. add ( registB) ; baseB1 =  Box. createHorizontalBox ( ) ; baseB1. add ( box1) ; baseB1. add ( Box. createHorizontalStrut ( 8 ) ) ; baseB1. add ( box2) ; baseB2 =  Box. createVerticalBox ( ) ; baseB2. add ( baseB1) ; baseB2. add ( Box. createVerticalStrut ( 10 ) ) ; baseB2. add ( box3) ; okB. addActionListener ( log) ; registB. addActionListener ( log) ; log. setaccountT ( accountT) ; log. setnameT ( nameT) ; log. setButton ( okB, registB) ; add ( baseB2) ; setLayout ( new  FlowLayout ( ) ) ; setBounds ( 200 , 150 , 400 , 300 ) ; setVisible ( true ) ; setTitle ( "用户登录界面" ) ; setDefaultCloseOperation ( DISPOSE_ON_CLOSE) ; } public  static  void  main ( String[ ]  args)  { login lo =  new  login ( ) ; } 
} 
package  注册信息;   import  java. awt. event. ActionEvent;   
import  java. awt. event. ActionListener;   
import  java. sql. Connection;   
import  java. sql. ResultSet;   
import  java. sql. Statement;   import  javax. swing. *;   public  class  register_db  extends  conn_db  implements  ActionListener {   JTextField textacc, textname;   JButton okButton, resetButton;   Statement stmt;   ResultSet rs;   
public  void  setaccountField ( JTextField a) {   textacc =  a;   }   public  void  setnameField ( JTextField n) {   textname =  n;   }   public  void  setokButton ( JButton b1) {   okButton =  b1;   }   public  void  setresetButton ( JButton b2) {   resetButton =  b2;   }   public  void  actionPerformed ( ActionEvent e) {   if ( e. getSource ( )  ==  okButton) {   if ( textacc. getText ( ) . equals ( "" ) )             JOptionPane. showMessageDialog ( null,  "请输入账号" , "警告对话框" , JOptionPane. WARNING_MESSAGE) ;   else  if ( textname. getText ( ) . equals ( "" ) )   JOptionPane. showMessageDialog ( null, "请输入姓名" , "警告对话框" , JOptionPane. WARNING_MESSAGE) ;   else {   String acc =  textacc. getText ( ) ;   String name =  textname. getText ( ) ;   try  {   connection ( ) ;   writeInSql ( acc, name) ;   }  catch  ( Exception  e1)  {   System. out. println ( "插入失败" ) ;   e1. printStackTrace ( ) ;   }   }   }   else  if ( e. getSource ( )  ==  resetButton) {   textacc. setText ( "" ) ;   textname. setText ( "" ) ;   }   }   void  writeInSql ( String acc, String name)  throws  Exception{   String sql;   Connection con =  super . con;   Statement stmt =  con. createStatement ( ) ;   sql =  "create table if not exists my(account varchar(10),name varchar(20))" ;   stmt. executeUpdate ( sql) ;   
System. out. println ( "创建表成功" ) ;       sql =  "insert into my(account,name) values('" + acc+ "','" + name+ "')" ;   int  rw =  stmt. executeUpdate ( sql) ;   
if ( rw <=  0 ) {              JOptionPane. showMessageDialog ( null, "注册失败" ) ;   }   else {   JOptionPane. showMessageDialog ( null,  "注册成功" ) ;   }   }   
} package  注册信息; import  javax. swing. JFrame; import  java. awt. FlowLayout; 
import  javax. swing. *; public  class  register  extends  JFrame { JLabel accountLabel, nameLabel; JButton okButton, resetButton; JTextField accountText, nameText; Box baseBox1, baseBox2,  box1, box2, box3; 		 register_db regist; register ( ) { init ( ) ; } void  init ( ) { setLayout ( new  FlowLayout ( ) ) ; accountLabel =  new  JLabel ( "账号" ) ; nameLabel=  new  JLabel ( "姓名" ) ; accountText =  new  JTextField ( 10 ) ; nameText =  new  JTextField ( 20 ) ; okButton =  new  JButton ( "确定" ) ; resetButton =  new  JButton ( "重置" ) ; regist =  new  register_db ( ) ; box1 =  Box. createVerticalBox ( ) ; box1. add ( accountLabel) ; box1. add ( Box. createVerticalStrut ( 8 ) ) ; box1. add ( nameLabel) ; box2 =  Box. createVerticalBox ( ) ; box2. add ( accountText) ; box2. add ( Box. createVerticalStrut ( 8 ) ) ; box2. add ( nameText) ; box3 =  Box. createHorizontalBox ( ) ; box3. add ( okButton) ; box3. add ( Box. createHorizontalStrut ( 15 ) ) ; box3. add ( resetButton) ; baseBox1 =  Box. createHorizontalBox ( ) ; baseBox1. add ( box1) ; baseBox1. add ( Box. createHorizontalStrut ( 8 ) ) ; baseBox1. add ( box2) ; baseBox2 =  Box. createVerticalBox ( ) ; baseBox2. add ( baseBox1) ; baseBox2. add ( Box. createVerticalStrut ( 10 ) ) ; baseBox2. add ( box3) ; add ( baseBox2) ; okButton. addActionListener ( regist) ; resetButton. addActionListener ( regist) ; regist. setaccountField ( accountText) ; regist. setnameField ( nameText) ; regist. setokButton ( okButton) ; regist. setresetButton ( resetButton) ; setBounds ( 200 , 200 , 400 , 300 ) ; setVisible ( true ) ; setDefaultCloseOperation ( DISPOSE_ON_CLOSE) ; setTitle ( "用户注册界面" ) ; } 
}