JDBC读取MySQL数据库,并通过socket连接传给客户端,实时更新

自己用Java写了个客户端通过服务器实时读取MySQL数据库的程序。JDBC实时读取数据库数据,并每隔1秒发送给客户端,客户端处于一直接收状态,测试用IP:127.0.0.1,端口号为7000。先运行服务器,再运行客户端。源程序如下:

服务端:

package server;
import java.io.*;
import java.net.*;
import java.sql.*;
public class Server{
	public static void main(String args[]){		
		while(true) {
		//socket
		ServerSocket server=null;
		Socket you=null;		
			try {	server =new ServerSocket(7000);
			} 
			catch (IOException el) {
				System.out.println("正在监听");
			}
			try {
				System.out.println("等待客户呼叫");	
				you=server.accept();
				System.out.println("客户的地址:"+you.getInetAddress());
			}
			catch(IOException e) {
				System.out.println("正在等待客户");
			}
			if(you!=null) {
				ServerThread threadForClient=new ServerThread(you);
				threadForClient.start();
			}
		}		
	}
}
class ServerThread extends Thread{
    static int id;
    static int name;
    static int password;
    static int email;
    static int birthday;        
	Socket socket;
	DataOutputStream out=null;
	DataInputStream in=null;
	String s=null;	    
	ServerThread(Socket t){
		socket=t;
			try {
				out=new DataOutputStream(socket.getOutputStream());
				in=new DataInputStream(socket.getInputStream());
			} catch (IOException e) {}
	}
	public void run() {
		//JDBC初始化
		// JDBC 驱动名及数据库 URL
	    final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	    final String DB_URL = "jdbc:mysql://localhost:3306/data?useSSL=true";	 
	    // 数据库的用户名与密码,需要根据自己的设置
	    final String USER = "root";
	    final String PASS = "root";
	    //jdbc
	    Connection conn = null;
	    Statement stmt = null;
		while(true) {
			try {
				double r=in.readDouble();

				// 注册 JDBC 驱动
		        Class.forName("com.mysql.jdbc.Driver");
		        // 打开链接
		        System.out.println("连接数据库...");
		        conn = DriverManager.getConnection(DB_URL,USER,PASS);
		    	// 执行查询
		        System.out.println(" 实例化Statement对象...");
		        stmt = conn.createStatement();
				String sql;
		        sql = "SELECT id,name,password,email,birthday FROM data";
				ResultSet rs = stmt.executeQuery(sql);
				if(rs.last()){//只读取最后一个ID
		            // 通过字段检索
		            id = rs.getInt("id");
		            name = rs.getInt("name");
		            password  = rs.getInt("password");
		            email = rs.getInt("email");
		            birthday = rs.getInt("birthday");		            
		        }	
				out.writeDouble(ServerThread.id);
				out.writeDouble(ServerThread.name);
				out.writeDouble(ServerThread.password);
				out.writeDouble(ServerThread.email);
				out.writeDouble(ServerThread.birthday);					
			}catch(SQLException se){
		        // 处理 JDBC 错误
		        se.printStackTrace();
		    }catch(Exception e){
		        // 处理 Class.forName 错误
		        e.printStackTrace();
		    } 
			try{
				Thread.sleep(1000);
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

客户端:

package server;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client{	
	public static void main(String args[]){				
		Scanner scanner=new Scanner(System.in);
		Socket mysocket=null;
		DataInputStream in=null;
		DataOutputStream out=null;
		Thread readData;
		Read read=null;
		try {	mysocket=new Socket();
				read=new Read();
				readData=new Thread(read);
				System.out.print("输入服务器的IP:");
				String IP=scanner.nextLine();
				System.out.print("输入端口号:");
				int port=scanner.nextInt();				
					InetAddress address = InetAddress.getByName(IP);
					InetSocketAddress socketAddress = new InetSocketAddress(address, port);
					mysocket.connect(socketAddress);
					in = new DataInputStream(mysocket.getInputStream());
					out = new DataOutputStream(mysocket.getOutputStream());
					read.setDataInputStream(in);
					readData.start();	
		}
		catch(Exception e) {
			System.out.println("服务器已断开"+e);
		}
		System.out.println("接收数据:");	
		while(true) {
			double radius=0;
			try {
				out.writeDouble(radius);
			} 
			catch (Exception e) {}	
		}
	}
}
class Read implements Runnable{
	DataInputStream in;
	public void setDataInputStream(DataInputStream in) {
		this.in=in;
	}
	public void run() {
		double id=0;	
		double name=0;
		double password=0;
		double email=0;
		double birthday=0;		
		while (true) {
			try {
				id=in.readDouble();
				System.out.println("id:"+id);
				name=in.readDouble();
				System.out.println("名字:"+name);
				password=in.readDouble();
				System.out.println("密码:"+password);
				email=in.readDouble();
				System.out.println("邮箱:"+email);
				birthday=in.readDouble();
				System.out.println("生日:"+birthday);		
								
				}				
			catch (IOException e) {				
				e.printStackTrace();
			} 			 
		} 			
	}	
}

MySQL数据如下(只有数字),每隔1秒读取最后一行数据: 

猜你喜欢

转载自blog.csdn.net/qq1922631820/article/details/86405029