编写服务端工具类,MySQL连接及登陆信息验证,及int和byte之间的转化
MySQL用户信息表结构:
1 package com.netty.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class LoginConnetDB { 8 private static Connection ct = null; 9 private static LoginConnetDB instance = null; 10 11 public static LoginConnetDB getInstance() { 12 if (instance == null) { 13 instance = new LoginConnetDB(); 14 instance.init(); 15 } 16 return instance; 17 } 18 19 public void init() { 20 try { 21 getConnet(); 22 } catch (Exception e) { 23 24 } 25 } 26 27 public Connection getConnet() { 28 try { 29 30 if (ct == null) { 31 String url = "jdbc:mysql://101.200.55.192/armorcraft?user=DBUsername&password=DBPassword&useUnicode=true&characterEncoding=UTF-8"; 32 Class.forName("com.mysql.jdbc.Driver").newInstance(); 33 ct = DriverManager.getConnection(url); 34 } 35 } catch (Exception ex) { 36 ex.printStackTrace(); 37 } 38 return ct; 39 } 40 41 public void close() { 42 if (ct != null) { 43 try { 44 ct.close(); 45 } catch (SQLException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } 50 } 51 }
1 package com.netty.util; 2 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 public class VerifyOperate { 8 9 private PreparedStatement ps = null; 10 private ResultSet rs = null; 11 12 private static VerifyOperate instance; 13 14 public static VerifyOperate getInstance() 15 { 16 if (instance == null) 17 { 18 instance = new VerifyOperate(); 19 instance.init(); 20 } 21 return instance; 22 } 23 public void init() 24 { 25 try 26 { 27 28 } 29 catch (Exception e) 30 { 31 32 } 33 } 34 35 public int checkPlayer(String tableName,String emailOrPhone,String password) 36 { 37 //1表示用户名错误、2表示密码错误、-1表示其他错误、>10000 表示成功、 38 int re=-1; 39 try 40 { 41 String sql = null; 42 if(emailOrPhone.contains("@")) 43 { 44 sql = "select * from "+tableName+" where email='"+emailOrPhone+"'"; 45 } 46 else 47 { 48 sql = "select * from "+tableName+" where phone='"+emailOrPhone+"'"; 49 } 50 ps = LoginConnetDB.getInstance().getConnet().prepareStatement(sql); 51 rs=ps.executeQuery(); 52 53 if(rs.next()) 54 { 55 String dbPasswd = rs.getString(2); 56 57 if(dbPasswd.equals(password)) 58 { 59 //登入成功 60 re = rs.getInt(1); 61 } 62 else 63 { 64 //密码错误 65 re = 2; 66 } 67 } 68 else 69 { 70 //用户名错误 71 re = 1; 72 } 73 } 74 catch(Exception ex) 75 { 76 ex.printStackTrace(); 77 } 78 finally 79 { 80 try { 81 if(rs!=null) 82 { 83 84 rs.close(); 85 rs = null; 86 } 87 if(ps!=null) 88 { 89 90 ps.close(); 91 ps = null; 92 } 93 } catch (SQLException e) { 94 95 e.printStackTrace(); 96 } 97 } 98 99 return re; 100 } 101 102 public void close() 103 { 104 try 105 { 106 if(rs!=null) 107 { 108 rs.close(); 109 rs = null; 110 } 111 if(ps!=null) 112 { 113 ps.close(); 114 ps = null; 115 } 116 } 117 catch(Exception ex) 118 { 119 ex.printStackTrace(); 120 } 121 } 122 }
1 package com.netty.util; 2 /* 3 * int和byte之间的转化 4 */ 5 public class CoderUtil { 6 7 public static int bytesToInt(byte[] data, int offset) 8 { 9 int num = 0; 10 11 for(int i = offset; i < offset + 4; i++) 12 { 13 num <<= 8; 14 num |= (data[i] & 0xff); 15 } 16 17 return num; 18 } 19 20 public static byte[] intToBytes(int num) 21 { 22 byte[] b = new byte[4]; 23 24 for(int i= 0; i < 4; i++) 25 { 26 b[i] = (byte)(num >>> (24 - i * 8)); 27 } 28 29 return b; 30 } 31 }