📄 threadserverusewaterelec.java
字号:
/**
对使用的度数修改
端口号6004
filename:ThreadServerUseWaterElec.java
*/
//引入需要用到的包
import java.io.*;
import java.net.*;
import java.sql.*;
public class ThreadServerUseWaterElec extends Thread
{
//定义保存输入值的变量
private int donghao;//栋号
private int room;//寝室号
private int month;//月份
private double waterstop;//水的终止度数
private double elecstop;//电的终止度数
private String function=null;//电的终止度数
//连接数据库用数据源名
private String conname=null;
//定义返回值
boolean flag=false;
//定义网络连接对象
Socket sc=null;
DataInputStream in=null ;
DataOutputStream out=null;
ConnectionShuJuKu c=null;
//构造函数
ThreadServerUseWaterElec(Socket sc,String conname)
{
//接收传递过来的值
this.sc=sc;
this.conname=conname;
try
{
in=new DataInputStream(sc.getInputStream());
out=new DataOutputStream(sc.getOutputStream());
}//end try
catch(IOException er)
{
System.out.println(er.getMessage());
}
}//end 构造方法
public void run()
{
//读取数据
try
{
function=in.readUTF();
}
catch(IOException re)
{
System.out.println(re.getMessage());
}
//处理数据,调用存储过程来处理
try
{
//创建连接对象.并传值数据源名
c=new ConnectionShuJuKu(conname);
Connection con=c.getConnection();
//判断执行那部分功能
if(function.equals("queryuse"))
{
try
{
donghao=in.readInt();
room=in.readInt();
//有参数
CallableStatement cs=con.prepareCall("{Call queryusewaterelecproc(?,?)}");
//设置参数值
cs.setInt(1,donghao);
cs.setInt(2,room);
ResultSet rs=cs.executeQuery();
if(rs.next())
{
month=rs.getInt(1);
waterstop=rs.getDouble(2);
elecstop=rs.getDouble(3);
flag=true;
}
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
if(flag)
{
out.writeInt(month);
out.writeDouble(waterstop);
out.writeDouble(elecstop);
}
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end
else if(function.equals("changeuse"))
{
try
{
donghao=in.readInt();
room=in.readInt();
waterstop=in.readDouble();
elecstop=in.readDouble();
//有参数
CallableStatement cs=con.prepareCall("{Call changeusewaterelecproc(?,?,?,?)}");
//设置参数值
cs.setInt(1,donghao);
cs.setInt(2,room);
cs.setDouble(3,waterstop);
cs.setDouble(4,elecstop);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
}//enb try
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end changeuse
else if(function.equals("inputuse"))
{
try
{
donghao=in.readInt();
room=in.readInt();
waterstop=in.readDouble();
elecstop=in.readDouble();
//有参数
CallableStatement cs=con.prepareCall("{Call changeusewaterelecproc(?,?,?,?)}");
//设置参数值
cs.setInt(1,donghao);
cs.setInt(2,room);
cs.setDouble(3,waterstop);
cs.setDouble(4,elecstop);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
}//enb try
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end inputuse
else if(function.equals("inputnext"))//inputnext
{
flag=false;//还原为初始值
try
{
donghao=in.readInt();
room=in.readInt();
//有参数
CallableStatement cs=con.prepareCall("{Call queryusewaterelecproc(?,?)}");
//设置参数值
cs.setInt(1,donghao);
cs.setInt(2,room);
ResultSet rs=cs.executeQuery();
if(rs.next())
{
room=rs.getInt(4);
flag=true;
}
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
if(flag)
{
out.writeInt(room);
}
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end
else //controlmonth
{
flag=false;//还原为初始值
Month mo=new Month();//获取月份的类
CallableStatement cs=con.prepareCall("{Call controlmonthweproc}");
ResultSet rs=cs.executeQuery();
int row=0;//保存行数
while(rs.next())
{
row++;
}
int use[][]=new int[row][2];
double use2[][]=new double[row][2];
rs=cs.executeQuery();
int i=0;
while(rs.next())
{
donghao=rs.getInt(1);
room=rs.getInt(2);
month=rs.getInt(3);
waterstop=rs.getDouble(4);
elecstop=rs.getDouble(5);
use[i][0]=donghao;
use[i][1]=room;
use2[i][0]=waterstop;
use2[i][1]=elecstop;
i++;
}//end while
//控制月份(月份=年+月 如200607月)
mo.setMonth(month);
month=mo.getMonth();
//向使用情况表写入数据
for(int j=0;j<row;j++)
{
CallableStatement cs2=con.prepareCall("{Call inputnextmonthweproc(?,?,?,?,?)}");
cs2.setInt(1,use[j][0]);
cs2.setInt(2,use[j][1]);
cs2.setInt(3,month);
cs2.setDouble(4,use2[j][0]);
cs2.setDouble(5,use2[j][1]);
int x=cs2.executeUpdate();
//判断是否修改成功
if(x==1)
flag=true;
else
{
flag=false;
break;
}
}//end for
//向缴费表里写入数据
for(int j=0;j<row;j++)
{
CallableStatement cs2=con.prepareCall("{Call inputnextmonthjiaofeeproc(?,?,?)}");
cs2.setInt(1,use[j][0]);
cs2.setInt(2,use[j][1]);
cs2.setInt(3,month);
int x=cs2.executeUpdate();
//判断是否修改成功
if(x==1)
flag=true;
else
{
flag=false;
break;
}
}//end for
//向客户端写入数据
try
{
out.writeBoolean(flag);
}
catch(IOException asdf){}
}//end
con.close();
}//end try
catch(SQLException sqle)
{
System.out.println("sql"+sqle.getMessage());
}
//关闭对象
try
{
in.close();
out.close();
sc.close();
}
catch(Exception e){}
}//end run
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -