1 package Test; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 public class DBUtil { 10 11 public static String db_url = "jdbc:mysql://localhost:3306/first?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"; 12 public static String db_user = "root"; 13 public static String db_pass = "123456"; 14 15 public static Connection getConn () { 16 Connection conn = null; 17 18 try { 19 Class.forName("com.mysql.jdbc.Driver"); 20 conn = DriverManager.getConnection(db_url, db_user, db_pass); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 25 return conn; 26 }//end getConn 27 28 public static void close (Statement state, Connection conn) { 29 if (state != null) { 30 try { 31 state.close(); 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 if (conn != null) { 38 try { 39 conn.close(); 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 public static void close (ResultSet rs, Statement state, Connection conn) { 47 if (rs != null) { 48 try { 49 rs.close(); 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } 53 } 54 55 if (state != null) { 56 try { 57 state.close(); 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 if (conn != null) { 64 try { 65 conn.close(); 66 } catch (SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 public static void main(String[] args) throws SQLException { 73 Connection conn = getConn(); 74 PreparedStatement pstmt = null; 75 ResultSet rs = null; 76 String sql ="select * from user"; 77 pstmt = conn.prepareStatement(sql); 78 rs = pstmt.executeQuery(); 79 if(rs.next()){ 80 System.out.println("连接成功"); 81 }else{ 82 System.out.println("连接失败"); 83 } 84 } 85 }
1 package Test; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 public class LoginForm extends JFrame { 8 9 private static final long serialVersionUID = 1L; 10 private UserDao dao = new UserDao(); 11 //设置按钮组件 12 13 private JButton jb=new JButton("登录"); //添加一个登录按钮 14 private JButton button=new JButton("重置"); //添加一个确定按钮 15 //设置文本框组件 16 private JTextField username = new JTextField();//用户名框 17 private JPasswordField password = new JPasswordField();//密码框:为加密的*** 18 19 JLabel user_name=new JLabel("账号:");//设置左侧用户名文字 20 JLabel pass_word=new JLabel("密码:");//设置左侧密码文字 21 22 public void init() 23 { 24 /* 组件绝对位置 */ 25 user_name.setBounds(50, 70, 300, 25); 26 pass_word.setBounds(50, 130, 200, 25); 27 28 username.setBounds(110, 70, 300, 25);//设置用户名框的宽,高,x值,y值 29 password.setBounds(110, 130, 300, 25);//设置密码框的宽,高,x值,y值 30 31 button.setBounds(315, 225, 90, 20);//设置确定按钮的宽,高,x值,y值 32 jb.setBounds(95, 225, 90, 20);//设置确定按钮的宽,高,x值,y值 33 34 35 /* 组件透明化*/ 36 user_name.setOpaque(false); 37 pass_word.setOpaque(false); 38 39 40 //监听事件 41 jb.addActionListener(new ActionListener(){ //为确定按钮添加监听事件 42 43 @SuppressWarnings("deprecation") 44 public void actionPerformed(ActionEvent arg0) { 45 validate(username.getText().trim(),password.getText().trim()); 46 } 47 }); 48 49 50 //重置按钮 51 button.addActionListener(new ActionListener(){ //为重置按钮添加监听事件 52 //同时清空name、password的数据 53 public void actionPerformed(ActionEvent arg0) { 54 // TODO 自动生成方法存根 55 username.setText(""); 56 password.setText(""); 57 } 58 }); 59 60 } 61 62 public void display() 63 { 64 JFrame f =new JFrame(); 65 f.setTitle("登录页面"); 66 //窗口退出行为 67 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 //设置窗口大小不可变 69 f.setResizable(false); 70 //设置窗口打开居中 71 f.setLocationRelativeTo(null); 72 //窗口大小 73 f.setSize(500, 300); 74 75 init(); 76 77 //添加组件 78 Container contentPanel= new Container();//添加一个contentPanel容器 79 contentPanel.setLayout(null);//设置添加的contentPanel容器为流布局管理器 80 contentPanel.add(user_name); 81 contentPanel.add(pass_word); 82 contentPanel.add(username); 83 contentPanel.add(password); 84 contentPanel.add(jb); 85 contentPanel.add(button); 86 87 f.add(contentPanel); 88 //展示窗口 89 f.setVisible(true); 90 } 91 92 93 public void validate(String username,String password) 94 { 95 96 if(username.trim().length()==0||password.trim().length()==0){ 97 JOptionPane.showMessageDialog(null, "用户名,密码不允许为空"); 98 99 return; 100 } 101 102 if(dao.findUser(username, password)) 103 { 104 105 JOptionPane.showMessageDialog(null, "登录成功!"); 106 107 108 }else { 109 JOptionPane.showMessageDialog(null, "用户名或密码错误"); 110 111 } 112 113 } 114 115 }
1 package Test; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class UserDao { 9 public boolean findUser(String username, String password) { 10 //准备SQL语句 11 String sql = "select * from user where username ='" + username + "'"; 12 Connection conn= DBUtil.getConn(); 13 //创建语句传输对象 14 Statement state = null; 15 ResultSet rs= null; 16 int flag=0; 17 String c_password=null; 18 try { 19 state = conn.createStatement(); 20 rs = state.executeQuery(sql); 21 while(rs.next()) { 22 ++flag; 23 c_password=rs.getString("password"); 24 } if (flag == 0) { 25 return false; 26 } 27 if (!password.equals(c_password)) { //判断密码 28 return false; 29 } 30 }catch (SQLException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 }finally { 34 DBUtil.close(rs, state, conn); 35 } 36 return true; 37 } 38 }