修复Json无法解析的问题,添加Upload的支持

String.Trim()方法能够去除字符串周围的不正常字符
This commit is contained in:
Kirigaya Kazuto 2017-03-09 12:44:45 +08:00
parent b6e07fabdd
commit 1013b1de70

View File

@ -34,6 +34,7 @@ class Handle implements Runnable
} }
String str = new String(buf,0,len); String str = new String(buf,0,len);
str=new String(str.trim());
System.out.println("String is: "+str); System.out.println("String is: "+str);
@ -93,18 +94,40 @@ class Handle implements Runnable
} }
break; break;
case "getpart": case "getpart":
{
}
break; break;
case "getfull": case "getfull":
{
}
break; break;
case "upload": case "upload":
{
UploadData up=null;
try
{
up=gson.fromJson(str,UploadData.class);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Incorrect Upload Data");
continue;
}
int ret=BookFloatingServer.Upload(up.getBookname(),up.getAuthor(),up.getContext(),up.getImageURL());
pw.write("{'ret':'"+ret+"'}");
pw.flush();
}
break;
default:
System.out.println("Unknown Type");
break; break;
} }
} }
String reply="Connection is OK";
pw.write(reply);
pw.flush();
} }
catch(Exception e) catch(Exception e)
{ {
@ -126,6 +149,9 @@ class Handle implements Runnable
public class BookFloatingServer public class BookFloatingServer
{ {
public static int Login(String Email,String Password) { public static int Login(String Email,String Password) {
System.out.println("*Database*: Login with "+Email+" "+Password);
Connection conn = null; Connection conn = null;
String url="jdbc:mysql://gameharbor.cn:3306/test"; String url="jdbc:mysql://gameharbor.cn:3306/test";
Statement stmt = null; Statement stmt = null;
@ -179,6 +205,8 @@ public class BookFloatingServer
} }
public static int Register(String Email,String Password,String Nickname,String School){ public static int Register(String Email,String Password,String Nickname,String School){
System.out.println("*Database*:Register with "+Email+" "+Password+" "+Nickname+" "+School);
int upret=-1; int upret=-1;
Connection conn=null; Connection conn=null;
String url="jdbc:mysql://gameharbor.cn:3306/test"; String url="jdbc:mysql://gameharbor.cn:3306/test";
@ -214,6 +242,52 @@ public class BookFloatingServer
return upret; return upret;
} }
public static int Upload(String Bookname,String Author,String Context,String ImageURL){
System.out.println("*Database*: Upload with "+Bookname+" "+Author+" "+Context+" "+ImageURL);
int ret=-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.book VALUES (?,?,?,?,?)");
stmt=conn.prepareStatement(sqlString,Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, Bookname);
stmt.setString(2,Author);
stmt.setString(3,"Provider Here");
stmt.setString(4, ImageURL);
stmt.setString(5, Context);
ret=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 ret;
}
public static FetchInfo Fetch()
{
FetchInfo ff=null;
return ff;
}
public static void main(String[] args) { public static void main(String[] args) {
/* /*
try { try {