threadserverroomstudent.java
来自「对学生基本信息进行操作,添加,修改,删除」· Java 代码 · 共 277 行
JAVA
277 行
/**
对学生基本信息进行操作
添加,修改,删除
使用端口号6002
filename:ThreadServerRoomStudent.java
*/
//引入需要用到的包
import java.io.*;
import java.net.*;
import java.sql.*;
public class ThreadServerRoomStudent extends Thread
{
//定义保存读取数据的变量
private String function=null;//功能
private String xuehao=null;//学号
private String name=null;//姓名
private String sex=null;//性别
private String partment=null;//系别
private String banji=null;//班级
private int donghao;//栋号
private int room;//寝室号
private int tel;//电话号码
private int chuangsum;//电话号码
//连接数据库用数据源名
String conname=null;
//定义网络连接对象
Socket sc=null;
DataInputStream in=null ;
DataOutputStream out=null;
ConnectionShuJuKu c=null;
//构造函数
ThreadServerRoomStudent(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()
{
//定义返回值
boolean flag=false;
//读取数据
try
{
function=in.readUTF();
}
catch(IOException re)
{
System.out.println(re.getMessage());
}
//处理数据,调用存储过程来处理
try
{
//创建连接对象.并传值数据源名
c=new ConnectionShuJuKu(conname);
Connection con=c.getConnection();
//判断执行那部分功能
if(function.equals("addroom"))
{
try
{
donghao=in.readInt();
room=in.readInt();
chuangsum=in.readInt();
//有参数
CallableStatement cs=con.prepareCall("{Call addroomproc(?,?,?)}");
//设置参数值
cs.setInt(1,donghao);
cs.setInt(2,room);
cs.setInt(3,chuangsum);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end addroom
else if(function.equals("delroom"))
{
try
{
donghao=in.readInt();
room=in.readInt();
//有参数
CallableStatement cs=con.prepareCall("{Call delroomproc(?,?)}");
//设置参数值
cs.setInt(1,donghao);
cs.setInt(2,room);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end delroom
else if(function.equals("addstudent"))
{
try
{
xuehao=in.readUTF();
name=in.readUTF();
sex=in.readUTF();
partment=in.readUTF();
banji=in.readUTF();
donghao=in.readInt();
room=in.readInt();
tel=in.readInt();
//有参数
CallableStatement cs=con.prepareCall("{Call addstudentproc(?,?,?,?,?,?,?,?)}");
//设置参数值
cs.setString(1,xuehao);
cs.setString(2,name);
cs.setString(3,sex);
cs.setString(4,partment);
cs.setString(5,banji);
cs.setInt(6,donghao);
cs.setInt(7,room);
cs.setInt(8,tel);
int row=cs.executeUpdate();
//判断是否修改成功
if(row==1)
flag=true;
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
}
catch(IOException re2)
{
System.out.println(re2.getMessage());
}
}//end
else if(function.equals("querystudent"))
{
try
{
xuehao=in.readUTF();
CallableStatement cs=con.prepareCall("{Call querystudentproc(?)}");
//设置参数值
cs.setString(1,xuehao);
ResultSet rs=cs.executeQuery();
if(rs.next())
{
name=rs.getString("姓名");
sex=rs.getString("性别");
donghao=rs.getInt("栋号");
room=rs.getInt("寝室号");
tel=rs.getInt("电话号码");
flag=true;
}
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
if(flag)
{
out.writeUTF(name);
out.writeUTF(sex);
out.writeInt(donghao);
out.writeInt(room);
out.writeInt(tel);
}//end if
}//end try
catch(IOException eqwew){}
}
else if(function.equals("changestudent"))
{
try
{
xuehao=in.readUTF();
donghao=in.readInt();
room=in.readInt();
tel=in.readInt();
//有参数
CallableStatement cs=con.prepareCall("{Call changestudentproc(?,?,?,?)}");
//设置参数值
cs.setString(1,xuehao);
cs.setInt(2,donghao);
cs.setInt(3,room);
cs.setInt(4,tel);
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 changestudent
else //del
{
try
{
xuehao=in.readUTF();
CallableStatement cs=con.prepareCall("{Call delstudentproc(?)}");
//设置参数值
cs.setString(1,xuehao);
int row=cs.executeUpdate();
if(row==1)
flag=true;
else
flag=false;
//向客户端写入数据
out.writeBoolean(flag);
}//end try
catch(IOException eqwew){}
}//end del
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 + =
减小字号Ctrl + -
显示快捷键?