Add 'BookFloatingServer.java'

master
Kirigaya Kazuto 2017-03-06 21:47:55 +08:00
parent 96eeb40c99
commit d535d75a3d
1 changed files with 179 additions and 0 deletions

179
BookFloatingServer.java Normal file
View File

@ -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();
}
}
}
*/