📄 server.java
字号:
case 6: //查看个人资料
{
name = new String(in.readLine());
String id1 = new String();
String account1 = new String();
String question1 = new String();
PrintWriter toClient5 = null;
if(Query.select(name))
{
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
ResultSet rv=null;
String s = new String();
String v=new String();
String host="localhost";
String username="root";
String password1="111111";
String database="shoppingmall";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
try
{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch(Exception e)
{
System.out.println("Error loading Mysql Driver!");
}
try
{
conn = DriverManager.getConnection(url,username,password1);
stmt = conn.createStatement(); //Statement对象用于将sql语句发送到数据库中
rs = stmt.executeQuery("Select id FROM user where name = '"+name +"'");
while(rs.next())
{
id1 = rs.getString("id").trim();
}
rs = stmt.executeQuery("Select account FROM user where name = '"+name +"'");
while(rs.next())
{
account1 = rs.getString("account").trim();
}
rs = stmt.executeQuery("Select question FROM user where name = '"+name +"'");
while(rs.next())
{
question1 = rs.getString("question").trim();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
try
{ //返回结果
Socket isToClient5 = new Socket("localhost", 2013);
toClient5 = new PrintWriter(isToClient5.getOutputStream(),true);
toClient5.println(id1);
toClient5.println(account1);
toClient5.println(question1);
}
catch(IOException ex)
{
System.err.println(ex);
}
toClient5.flush();
}
case 7: //修改个人资料
{
name = new String(in.readLine());
String newaccount = new String(in.readLine());
String oldpassword = new String(in.readLine());
String newpassword = new String(in.readLine());
DataOutputStream toClient5 = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
String s = new String();
String host="localhost";
String username="root";
String password1="111111";
String database="shoppingmall";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
if(oldpassword.length()!=0)
{
try{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}catch(Exception e){
System.out.println("Error loading Mysql Driver!");
}
try{
conn = DriverManager.getConnection(url,username,password1);
System.out.println("Success connect Mysql Database!1");
stmt = conn.createStatement(); //Statement对象用于将sql语句发送到数据库中
rs = stmt.executeQuery("Select password FROM user where name = '"+name +"'");
while(rs.next())
{
s = rs.getString("password").trim();
}
if(!s.equals(oldpassword))
result3 = -1;
conn.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
}
if(result3==1)
{
if(newaccount.length()==0)
Change.updateUser1(name, newpassword);//只修改密码
else if(newpassword.length()==0)
Change.updateUser2(name, newaccount);//只修改账户号码
else
Change.updateUser(name, newaccount, newpassword);//全部修改
jta.append("用户"+name+"修改成功");
}
try
{ //返回结果
Socket isToClient5 = new Socket("localhost", 2011);
toClient5 = new DataOutputStream(isToClient5.getOutputStream());
toClient5.writeInt(result3);
result3=1;
}
catch(IOException ex)
{
System.err.println(ex);
}
toClient5.flush();
}
case 8: //普通用户注册
{
name = new String(in.readLine());
password = new String(in.readLine());
id = new String(in.readLine());
account=new String(in.readLine());
question = new String(in.readLine());
answer = new String(in.readLine());
if(Query.select(name))//查看用户名是否存在
{
result = 0;
}
if(result == 1)
{
register.insertUser(name, password, id , question, answer,account,0,null);
}//不存在则插入user表
try
{//返回结果
Socket isToClient = new Socket("localhost", 2001);
DataOutputStream toClient = new DataOutputStream(isToClient.getOutputStream());
toClient.writeInt(result);
if(result==1)
jta.append("用户"+name+"注册成功");
result =1;
}
catch(IOException ex)
{
System.err.println(ex);
}
}
case 9://店主注册
{
name = new String(in.readLine());
password = new String(in.readLine());
id = new String(in.readLine());
account=new String(in.readLine());
question = new String(in.readLine());
answer = new String(in.readLine());
shopname=new String(in.readLine());
if(Query.select(name))//查看用户名是否存在
{
result1 = 0;
}
if(result1 == 1)
{
register.insertUser(name, password, id, question, answer, account,0,shopname);
}//不存在则插入user表
try
{//返回结果
Socket isToClient1 = new Socket("localhost", 2002);
DataOutputStream toClient1 = new DataOutputStream(isToClient1.getOutputStream());
toClient1.writeInt(result1);
jta.append("店主"+name+"注册成功");
result1=1;
}
catch(IOException ex)
{
System.err.println(ex);
}
}
case 10: //店主进店时修改状态
{
name = new String(in.readLine());
if(Query.select(name))
{
Change.updateUser(name, 1);
}
}
case 11: //确认购买
{
int r = 1;
name = new String(in.readLine());
String shopername = new String(in.readLine());
String goodsname = new String(in.readLine());
String sum = new String(in.readLine());
double sum1 = Double.parseDouble(sum);
DataOutputStream toClient4 = null;
if(!Query.state(shopername))//店主已离线,则取消交易
{
r=-1;
ChaRuCar.deletegoods(name, goodsname, shopername);//重新设定商店中商品的数量
Change1.updateUser(name, shopername,goodsname);//删除购物车中的商品
}
else{
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
ResultSet rv=null;
String s = new String();
String v=new String();
String host="localhost";
String username="root";
String password1="111111";
String database="shoppingmall";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
try
{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//System.out.println("Success loading Mysql Driver!");
}
catch(Exception e)
{
System.out.println("Error loading Mysql Driver!");
}
try
{ //修改各自账户的金额
conn = DriverManager.getConnection(url,username,password1);
stmt = conn.createStatement(); //Statement对象用于将sql语句发送到数据库中
rs = stmt.executeQuery("Select sum FROM user where name = '"+name +"'");
while(rs.next())
{
s = rs.getString("sum").trim();
}
double sum2 = Double.parseDouble(s);
if(sum2 < sum1)
{
r = 0;
ChaRuCar.deletegoods(name, goodsname, shopername);//重新设定商店中商品的数量
Change1.updateUser(name, shopername,goodsname);//删除购物车中的商品
}
else
{ Change1.updateUser(name, shopername,goodsname);
sum2 = sum2 - sum1;
Change.updateUser(name, sum2);
rs = stmt.executeQuery("Select sum FROM user where name = '"+shopername +"'");
while(rs.next())
{
s = rs.getString("sum").trim();
}
double sum3 = Double.parseDouble(s);
sum3 = sum3 + sum1;
Change.updateUser(shopername, sum3);
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
try
{ //返回结果
Socket isToClient4 = new Socket("localhost", 2019);
toClient4 = new DataOutputStream(isToClient4.getOutputStream());
toClient4.writeInt(r);
r=1;
}
catch(IOException ex)
{
System.err.println(ex);
}
toClient4.flush();
}
case 12: //初始化商店中的商品
{
int i = 0;
int j = 0;
int i1 = 0;
name = new String(in.readLine());
String[] s = new String[20];
String[] s1 = new String[20];
String[] s2 = new String[20];
PrintWriter toClient6 = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
ResultSet rv=null;
//String s = new String();
String v=new String();
String host="localhost";
String username="root";
String password1="111111";
String database="shoppingmall";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
try
{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//System.out.println("Success loading Mysql Driver!");
}
catch(Exception e)
{
System.out.println("Error loading Mysql Driver!");
}
try
{
conn = DriverManager.getConnection(url,username,password1);
stmt = conn.createStatement(); //Statement对象用于将sql语句发送到数据库中
rs = stmt.executeQuery("Select goodsname FROM goods where name = '"+name +"'");
while(rs.next())
{
s[i] = rs.getString("goodsname");
i ++;
}
rs = stmt.executeQuery("Select number FROM goods where name = '"+name +"'");
while(rs.next())
{
s1[j] = rs.getString("number");
j ++;
}
rs = stmt.executeQuery("Select price FROM goods where name = '"+name +"'");
while(rs.next())
{
s2[i1] = rs.getString("price");
i1 ++;
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
try
{ //返回结果
Socket isToClient6 = new Socket("localhost", 2015);
toClient6 = new PrintWriter(isToClient6.getOutputStream(),true);
int x = i;
toClient6.println(String.valueOf(i));
i = 0;
j = 0;
i1 = 0;
for(;i <= x;)
{
toClient6.println(s[i]);
toClient6.println(s1[j]);
toClient6.println(s2[i1]);
i ++;
j ++;
i1 ++;
}
}
catch(IOException ex)
{
System.err.println(ex);
}
toClient6.flush();
}
case 13: //初始化购物车
{
int i = 0;
int j = 0;
int i1 = 0;
int j1 = 0;
String []s = new String[20];
String []s1 = new String[20];
String []s2 = new String[20];
String []s3 = new String[20];
name = new String(in.readLine());
PrintWriter toClient6 = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
ResultSet rv=null;
//String s = new String();
String v=new String();
String host="localhost";
String username="root";
String password1="111111";
String database="shoppingmall";
String url = "jdbc:mysql://localhost:3306/shoppingmall";
try
{
//-------------加载驱动-------------------------
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//System.out.println("Success loading Mysql Driver!");
}
catch(Exception e)
{
System.out.println("Error loading Mysql Driver!");
}
try
{
conn = DriverManager.getConnection(url,username,password1);
//System.out.println("Success connect Mysql Database!");
stmt = conn.createStatement(); //Statement对象用于将sql语句发送到数据库中
rs = stmt.executeQuery("Select goodsname FROM shopcar where name = '"+name +"'and state = '0'");
while(rs.next())
{
//System.out.println(i);
s[i] = rs.getString("goodsname");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -