题目源于java作业
写的比较乱,有误指正谢谢
正经人谁用derby啊
客户端:
分为窗口、监视器、数据对象三部分
窗口:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Win extends JFrame implements ActionListener {
JButton a,b,c;
JTextField id,name;
JTextArea show;
Police police;
DataInputStream in=null;
DataOutputStream out=null;
String IP="127.0.0.1";
int port=2001;
Socket mysocket=null;
Thread readData;
Read read=null;
Win(){
setLayout(new FlowLayout());
setTitle("查找系统");
setBounds(100,100,650,200);
a=new JButton("连接服务器");
b=new JButton("发送学号");
c=new JButton("发送姓名");
id =new JTextField(10);
name=new JTextField(10);
show=new JTextArea(7,50);
police=new Police();
police.setJTextField(id,name);
add(a);
add(new JLabel("学号:"));
add(id);
add(b);
add(new JLabel("姓名:"));
add(name);
add(c);
add(show);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
a.addActionListener(this);
b.addActionListener(police);
c.addActionListener(police);
}
/*动作监视器方法,用于监视连接服务器按钮使用*/
@Override
public void actionPerformed(ActionEvent e) {
try {
mysocket=new Socket();
read=new Read();
readData=new Thread(read);
if(mysocket.isConnected()){}
else {
InetAddress address = InetAddress.getByName(IP);
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
mysocket.connect(socketAddress);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
police.setOut(out);
read.setDataInputStream(in);
read.setShow(show);
readData.start();
}
} catch (IOException ioException) {
JOptionPane.showMessageDialog(this,"服务器连接失败");
}
}
}
/*线程操作---向服务器接收数据流并显示收到内容*/
class Read implements Runnable{
DataInputStream in;
String result=null;
JTextArea show;
public void setShow(JTextArea show) {
this.show = show;
}
public void setDataInputStream(DataInputStream in){
this.in=in;
}
@Override
public void run() {
while (true){
try {
show.append(in.readUTF()+"\n");
} catch (IOException e) {
System.out.println("与服务器已断开"+e);
break;
}
}
}
}
监视器:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;
public class Police implements ActionListener {
JTextField id,name;
Student student;
DataOutputStream out;
/*引入两个文本框*/
public void setJTextField(JTextField id,JTextField name) {
this.id = id;
this.name = name;
}
public void setOut(DataOutputStream out) {
this.out = out;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("发送学号")){
student =new Student();
student.setId(id.getText());
try {
out.writeUTF("numb:"+ student.getId());//这里通过字符串连接给字符串加上前缀便于服务器进行功能分类
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
if(e.getActionCommand().equals("发送姓名")){
student =new Student();
student.setName(name.getText());
try {
out.writeUTF("name:"+ student.getName());//同上应该有更好的方法,但我懒得想了
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
数据对象:
public class Student {
String name;//姓名
String id;//学号
/*由于数据库表结构,这里可以加上date和double代表出生日期和成绩*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
服务器端
分为客户端接受口和数据库操作两个类
这里可以分得更细一点,但我懒得搞了 阿巴阿巴
客户端接受口:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Datebase datebase=new Datebase();
ServerSocket server = null;
ServerThread thread;
Socket you=null;
datebase.datebasecreate();
/*监视客户端*/
while (true){
try {
server=new ServerSocket(2001);
} catch (IOException e) {
System.out.println("正在监听");
}
try {
System.out.println("等待客户呼叫");
you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
} catch (IOException e) {
System.out.println("正在等待客户");
}
if(you!=null){
new ServerThread(you,datebase).start();
}
}
}
}
/*对客户端传入数据进行操作,并传出数据*/
class ServerThread extends Thread{
Datebase datebase;
Socket socket;
DataInputStream in =null;
DataOutputStream out=null;
String s=null;
ServerThread(Socket t,Datebase datebase){
this.datebase=datebase;
socket=t;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
} catch (IOException e) { }
}
@Override
public void run() {
String result=null;
while (true){
try {
String r=in.readUTF();//输入流转换
if(r.startsWith("name:")){//前缀检测
r=r.replace("name:","");//前缀去除
result=datebase.searchname(r);
}
if(r.startsWith("numb:")){
r=r.replace("numb:","");
result=datebase.searchid(r);
}
out.writeUTF(result);
} catch (IOException e) {
System.out.println("客户离开");
return;
}
}
}
}
数据库操作:
(这部分我没怎么分类,但实际上数据库操作应用几个类结合才好)
import java.sql.*;
public class Datebase {
Connection con=null;
ResultSet rs=null;
PreparedStatement sta=null;
String SQL=null;
public void datebasecreate(){
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
try {
con=DriverManager.getConnection("jdbc:derby:az;create=true");
System.out.println("连接成功");
} catch (Exception throwables) {
throwables.printStackTrace();
System.out.println(throwables);
}
try {
SQL="create table bate (id varchar(20) primary key, name varchar(30),birth date,javascore double)";
sta=con.prepareStatement(SQL);
sta.executeUpdate();
System.out.println("建表成功");
} catch (SQLException throwables) {
System.out.println("表已存在");
}
try {
/*向数据库插入数据来模拟完整数据库(可以再建个插入数据库的窗口来制成完整程序)*/
SQL="insert into bate values ('001','小王','2001-10-10',95),('002','小马','2002-05-12',80)";
sta=con.prepareStatement(SQL);
sta.executeUpdate();
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/*学号查询*/
public String searchid(String id){
try {
con=DriverManager.getConnection("jdbc:derby:az;create=false");
Statement statement;
statement=con.createStatement();
SQL="select * from bate where id = '"+id+"'";
rs=statement.executeQuery(SQL);
if(rs.next()){
String result="学号:"+rs.getString(1)
+"姓名:"+rs.getString(2)
+"出生日期:"+rs.getDate(3)
+"Java成绩:"+rs.getDouble(4);
con.close();
return result;
}
con.close();
return "为找到记录";
} catch (SQLException throwables) {
throwables.printStackTrace();
return "未找到记录1";
}
}
/*姓名查询*/
public String searchname(String name){
try {
con=DriverManager.getConnection("jdbc:derby:az;create=false");
SQL="select * from bate where name = '"+name+"'";
Statement statement;
statement=con.createStatement();
rs=statement.executeQuery(SQL);
if(rs.next()){
String result="学号:"+rs.getString(1)
+"姓名:"+rs.getString(2)
+"出生日期:"+rs.getDate(3)
+"Java成绩:"+rs.getDouble(4);
con.close();
return result;
}
con.close();
return "未找到记录";
} catch (SQLException throwables) {
throwables.printStackTrace();
return "未找到记录2";
}
}
}
/*这块代码重复率挺高的,有能力的可以优化一下,反正我是懒得搞了 狗头*/