📄 threadservercostandturn.java
字号:
/**
对单价进行操作
来对数据库的内容操作,
接受传递过来的网络连
接对象和数据源名,对
数据处理后返回给客户端
*/
//引入需要用到的包
import java.io.*;
import java.net.*;
import java.sql.*;
public class ThreadServerCostAndTurn extends Thread
{
private String bianhao=null;//编号
private double cost;//价格
private String function=null;//功能
private int donghao;//栋号
private String date=null;//日期
//连接数据库用数据源名
String conname=null;
//定义返回值
boolean flag=false;
//定义网络连接对象
Socket sc=null;
DataInputStream in=null ;
DataOutputStream out=null;
ConnectionShuJuKu c=null;
//构造函数
ThreadServerCostAndTurn(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("addcost"))
{
flag=false;
try
{
bianhao=in.readUTF();
cost=in.readDouble();
//调用有参数
CallableStatement cs=con.prepareCall("{Call addcostproc(?,?)}");
//设置参数值
cs.setString(1,bianhao);
cs.setDouble(2,cost);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
//向客户端写入数据
out.writeBoolean(flag);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end addcost
else if(function.equals("changecost"))
{
flag=false;
try
{
bianhao=in.readUTF();
cost=in.readDouble();
//调用有参数
CallableStatement cs=con.prepareCall("{Call changecostdproc(?,?)}");
//设置参数值
cs.setString(1,bianhao);
cs.setDouble(2,cost);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
//向客户端写入数据
out.writeBoolean(flag);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end changecost
else if(function.equals("querycost"))
{
flag=false;
try
{
bianhao=in.readUTF();
CallableStatement cs=con.prepareCall("{Call querycostproc(?) }");
cs.setString(1,bianhao);
ResultSet rs=cs.executeQuery();
//获取最大的行数
if(rs.next())
{
cost=rs.getDouble(1);
flag=true;
}
//向客户端写入数据
out.writeBoolean(flag);
if(flag)
out.writeDouble(cost);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}
else if(function.equals("clerklist"))
{
CallableStatement cs=con.prepareCall("{Call clerklistproc }");
ResultSet rs=cs.executeQuery();
//获取最大的行数
int row=0;
while(rs.next())
{
row++;
}
rs=cs.executeQuery();
//向客户端写入数据
try
{
//写入行数
out.writeInt(row);
//写入记录
String name=null;
while(rs.next())
{
//获取数据
bianhao=rs.getString(1);//编号
name=rs.getString(2);//姓名
//写入数据
out.writeBoolean(true);//使客户端继续执行
out.writeUTF(bianhao);
out.writeUTF(name);
}
//使客户端退出循环
out.writeBoolean(false);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end clerklist
else if(function.equals("addturn"))
{
flag=false;
try
{
bianhao=in.readUTF();
donghao=in.readInt();
date=in.readUTF();
CallableStatement cs=con.prepareCall("{Call addturnproc(?,?,?)}");
//设置参数值
cs.setString(1,bianhao);
cs.setInt(2,donghao);
cs.setString(3,date);
int row=cs.executeUpdate();
//判断是否添加成功
if(row==1)
flag=true;
//向客户端写入数据
out.writeBoolean(flag);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end addturn
else //personturn
{
flag=false;
try
{
bianhao=in.readUTF();
CallableStatement cs=con.prepareCall("{Call personturnproc(?) }");
cs.setString(1,bianhao);
ResultSet rs=cs.executeQuery();
//获取最大的行数
int row=0;
while(rs.next())
{
row++;
}
rs=cs.executeQuery();
//向客户端写入数据
out.writeInt(row);
//写入记录
String job=null;
while(rs.next())
{
//获取数据
donghao=rs.getInt(1);//栋号
date=rs.getString(2);//日期
job=rs.getString(3);
//写入数据
out.writeBoolean(true);//使客户端继续执行
out.writeInt(donghao);
out.writeUTF(date);
out.writeUTF(job);
}
//使客户端退出循环
out.writeBoolean(false);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end if
//关闭对象
con.close(); }//end try
catch(SQLException sqle)
{
System.out.println(sqle.getMessage());
}
//关闭对象
try
{
in.close();
out.close();
sc.close();
}
catch(Exception e){}
}//end run
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -