📄 server.java
字号:
import java.io.*;
import java.net.*;
import java.sql.*;
//import toyData;
//import toys;
//import UserData;
//import ioResultSet;
public class Server extends Thread
{
ServerSocket serverSocket;
//ResultSet result;
Connection con;
//String url;
String datasource;
toyData toydata;
toyList toylist;
// UserData userdata;
Statement toyStat;
ResultSet toyResult;
// ioResultSet toyResult;
public Server()
{
datasource=new String("jdbc:odbc:toydata");
// userdata=new UserData();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Server For User Login And Order Process");
System.out.println("Enter Your UserName:");
BufferedReader keyIn=new BufferedReader(new InputStreamReader(System.in));
String sUid=keyIn.readLine();
System.out.println("Enter Your Password:");
String pass=keyIn.readLine();
System.out.println("Testing Password :"+pass);
con=DriverManager.getConnection(datasource,sUid,pass);
//try
//{
serverSocket=new ServerSocket(1880);
System.out.println(serverSocket.toString());
}
catch(Exception e)
{
fail(e,"Could not start server");
}
getToydata();
System.out.println("Server started...");
this.start();
}
public void getToydata()
{
try
{
toyStat=con.createStatement();
//System.out.println("toystat");
//toyResult=new ioResultSet();
toyResult=toyStat.executeQuery("select * from toys");
//toyResult.result=toyRt;
//int cnt=50;
//System.out.println(cnt);
//toydata=new toyData();
//toydata.num=0;
toylist=new toyList();
int n=0;
toylist.toys=new toyData[50];
while(toyResult.next())
{
toylist.toys[n]=new toyData();
toylist.toys[n].toyid=toyResult.getString(1);
toylist.toys[n].toyname=toyResult.getString(2);
toylist.toys[n].toydesc=toyResult.getString(3);
toylist.toys[n].toyrate=toyResult.getFloat(4);
toylist.toys[n].toyimage=toyResult.getString(5);
toylist.num++;
n++;
}
/**********
while(toyResult.next())
{
System.out.println(toyResult.getString(1));
}
System.out.println(toyResult.getRow());
toydata=new toyData(num);
//toyResult.beforeFirst();
//for(n=0;n<num;n++)
//{
//System.out.println(toyResult.getString(1));
//Statement toySt=con.createStatement();
//ResultSet toyRt=toySt.executeQuery("select * from toys");
*************/
//System.out.println("end here ok");
}
catch(Exception ep)
{
fail(ep,"toy getting error");
}
}
// if someplace of the filecode error goto here ok......
public static void fail(Exception e,String str)
{
System.err.println(str+"."+e);
}
//the thread is actived
public void run()
{
try
{
while(true)
{
Socket client=serverSocket.accept();
new connect(client);
}
}
catch(IOException e)
{
fail(e,"Not Listening");
}
}
public static void main(String str[])
{
new Server();
}
// define the inner class (name:connect) of the class
class connect extends Thread
{
Socket netClient;
BufferedReader fromClient;
PrintStream toClient;
ObjectOutputStream toySender; //send toys details
//ObjectInputStream orderRec;//receive orders
UserData userdata;
PreparedStatement stat , orderStat;
ResultSet result;
String srt,ipAddress;
public connect(Socket client)
{
try
{
netClient=client;
//
//System.out.println(stat.toString()+","+orderStat.toString());
System.out.println("preparestament is ok");
}
catch(Exception ep)
{
System.out.println("dbms connecting failed");
}
try
{
fromClient=new BufferedReader(new InputStreamReader(netClient.getInputStream()));
toClient=new PrintStream(netClient.getOutputStream());
toySender=new ObjectOutputStream(netClient.getOutputStream());
//orderRec=new ObjectInputStream(netClient.getInputStream());
userdata=new UserData();
ipAddress=fromClient.readLine();
System.out.println(ipAddress+" connected");
}
catch(IOException e)
{
try
{
netClient.close();
toySender.close();
System.err.println("Unable to set up streams"+e);
return;
}
catch(IOException ex)
{
}
}
this.start();
}
public void returnMsg(String strmsg)
{
toClient.println(strmsg);
System.out.println(strmsg);
}
public void run()
{
//there is the center code of the server
//now i'll cut it to pieces
try{
//toClient.println("connect successful");
toySender.writeObject(toylist);
//toySender.close();
}
catch(Exception eo)
{
fail(eo,"i/o error");
}
System.out.println("now reading from client");
while(true)
{
try{
//Connection con1=DriverManager.getConnection(datasource,"sa","niuchao");
stat=con.prepareStatement
("select vpassword from userdetails where vusername=?");
String str=fromClient.readLine();
System.out.println("readed :"+ str);
if (str.equals("ok"))
{
System.out.println("execute login");
String strname=fromClient.readLine();
String strpass=fromClient.readLine();
//String sret;
System.out.println(strname+","+strpass);
stat.setString(1,strname);
result=stat.executeQuery();
//if (result.getRow()>0)
if (result.next())
{
String pwd=result.getString(1).trim();
System.out.println("real pass:"+pwd+".");
if(pwd.equals(strpass))
srt=new String("ok");
else
srt=new String("Password Error");
}
else
{
srt=new String("UserName is known");
// send message to client
}
//toClient.println(srt);
returnMsg(srt);
}
else
{
//Connection con2=DriverManager.getConnection(datasource,"sa","niuchao");
System.out.println("now read orders from client");
String ordersql="insert into OrderDetails (vusername,vtoybuy,vtraffic,vcasing,fmoneytotal,vaddress,vwithword) values(?,?,?,?,?,?,?)";
orderStat=con.prepareStatement(ordersql);
System.out.println("preparestatemn");
//userdata=(UserData)(orderRec.readObject());
/********* the code next page is Client use
toServer.println(userdata.username);
toServer.println(userdata.toybuy);
toServer.println(userdata.traff);
toServer.println(userdata.casing);
toServer.println(userdata.moneytotal);
toServer.println(userdata.address);
toServer.println(userdata.withword);
*********/
userdata.username=fromClient.readLine();
userdata.toybuy=fromClient.readLine();
userdata.traff=fromClient.readLine();
userdata.casing=fromClient.readLine();
userdata.moneytotal=new Float(fromClient.readLine()).floatValue();
userdata.address=fromClient.readLine();
userdata.withword=fromClient.readLine();
System.out.println("userdata readed");
/*****
UserName varchar(15),
dDate datetime,
vToyBuy varchar(150) ,
vTraffic varchar(20),
vCasing varchar(10),
fMoneyTotal float,
vAddress varchar(50),
vWithWord varchar(50)
****/
try{
orderStat.setString(1,userdata.username);
orderStat.setString(2,userdata.toybuy);
orderStat.setString(3,userdata.traff);
orderStat.setString(4,userdata.casing);
orderStat.setFloat(5,userdata.moneytotal);
orderStat.setString(6,userdata.address);
orderStat.setString(7,userdata.withword);
orderStat.executeUpdate();
srt=new String("Submit Successfully");
}
catch(SQLException sqle)
{
srt=sqle.toString();
//returnMsg(srt);
}
finally
{
returnMsg(srt);
}
//System.out.println(toydata.
}
}
catch(Exception re)
{
System.err.println(re);
srt=re.toString();
returnMsg(srt);
try{
netClient.close();
//System.out.println("netClient.close:");
System.out.println(ipAddress+" closed"+srt);
}
catch(Exception eee)
{
returnMsg(srt);
return;
}
return;
}
/*********
finally
{
toClient.println(srt);
}
********/
}//end while
}
}
}
// have't finished
//accept a text to finish the work submit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -