From d535d75a3d394ecf3c6579e86ed3641ecbd99d55 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Mon, 6 Mar 2017 21:47:55 +0800 Subject: [PATCH] Add 'BookFloatingServer.java' --- BookFloatingServer.java | 179 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 BookFloatingServer.java diff --git a/BookFloatingServer.java b/BookFloatingServer.java new file mode 100644 index 0000000..bce95fc --- /dev/null +++ b/BookFloatingServer.java @@ -0,0 +1,179 @@ +import java.sql.*; + +public class BookFloatingServer +{ + public static int Login(String Email,String Password) { + Connection conn = null; + String url="jdbc:mysql://gameharbor.cn:3306/test"; + Statement stmt = null; + ResultSet rs = null; + int ret=0; + + String RemotePassword = new String(); + try{ + conn=DriverManager.getConnection(url,"testuser","pwdtest"); + stmt=conn.createStatement(); + rs=stmt.executeQuery("SELECT pwass FROM user where email=\""+Email+"\""); + if(rs.next()) + { + RemotePassword=rs.getString(1); + } + + if(RemotePassword.equals(Password)) + { + ret=1;///Password Matchs + } + else + { + ret=0;///Password Does not Matchs + } + } catch(SQLException e) { + e.printStackTrace(); + ret=-1;// Connection Error + } + finally + { + try{ + if(rs!=null) rs.close(); + } catch(SQLException e) { + e.printStackTrace(); + } + + try{ + if(stmt!=null) stmt.close(); + } catch(SQLException e){ + e.printStackTrace(); + } + + try{ + if(conn!=null) conn.close(); + } catch(SQLException e) { + e.printStackTrace(); + } + } + + return ret; + } + + public static int Register(String Email,String Password,String Nickname,String School){ + int upret=-1; + Connection conn=null; + String url="jdbc:mysql://gameharbor.cn:3306/test"; + PreparedStatement stmt = null; + + try{ + conn=DriverManager.getConnection(url,"testuser","pwdtest"); + String sqlString=new String("INSERT into test.user VALUES (?,?,?,?,?,?)"); + stmt=conn.prepareStatement(sqlString,Statement.RETURN_GENERATED_KEYS); + stmt.setString(1,Email); + stmt.setString(2, Password); + stmt.setString(3, Nickname); + stmt.setString(4, School); + stmt.setString(5, ""); + stmt.setString(6, ""); + upret=stmt.executeUpdate(); + } catch(SQLException e) { + e.printStackTrace(); + } finally { + try { + if(stmt!=null) stmt.close(); + } catch(SQLException e) { + e.printStackTrace(); + } + + try{ + if(conn!=null) conn.close(); + } catch(SQLException e) { + e.printStackTrace(); + } + } + + return upret; + } + + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + System.exit(-1); + } + + /// ret=Login("test2@me.com","030201"); + int ret=Register("test3@me.com","030201","LiuTongYuan","QUST"); + System.out.println("Ret is "+ret); + } +} + +/* +import java.io.*; +import java.net.*; + +public class BookFloatingServer { + public class ServerThread implements Runnable + { + Socket s=null; + BufferedReader br=null; + public ServerThread(Socket s) + { + System.out.println("Thread Ready, ID="+Thread.currentThread().getId()); + this.s=s; + try + { + br=new BufferedReader( + new InputStreamReader( + s.getInputStream()) + ); + } + catch(Exception e) + { + e.printStackTrace(); + return; + } + } + public void run() + { + System.out.println("Thread Start, ID="+Thread.currentThread().getId()); + try + { + s.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + + System.out.println("Thread Over, ID="+Thread.currentThread().getId()); + } + } + public static void main(String[] args) + { + ServerSocket ss; + try + { + ss=new ServerSocket(50001); + } + catch(Exception e) + { + e.printStackTrace(); + return; + } + + while(true) + { + Socket s; + try + { + s=ss.accept(); + } + catch(Exception e) + { + e.printStackTrace(); + continue; + } + + new Thread(new ServerThread(s)).start(); + } + } +} +*/ \ No newline at end of file