【Java TCP/IP Socket】服务端的实现(可连接oracle数据库)

欢迎关注本人公众号交流学习

【Java TCP/IP Socket】服务端的实现(可连接oracle数据库)_第1张图片


1.Socket服务端实现步骤

1.创建ServerSocket对象,绑定并监听端口

2.通过accept监听客户端的请求

3.建立连接后,通过输出输入流进行读写操作

4.将读取的信息存入数据库,并进行下一次的监听

2.代码实现


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC操作数据库的工具类
* @ClassName: DBUtil 
 */
public class DBUtil {
	String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	String userName="system";
	String password ="oracledba";
	private Connection conn=null;
	private Statement stmt=null;
	private PreparedStatement pstmt=null;
	private ResultSet resultSet =null;
	
	static{
		//第一步:加载驱动
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取连接的工具方法
	 * @return
	 */
	public  Connection   getConn(){
			try {
			 //第二步:获取连接
			    conn =DriverManager.getConnection(url, userName, password);
			    return conn;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return null;
	}
	
	/**
	 * 获取语句对象的工具方法
	 * @return
	 */
	public  Statement getStatement(){
		
		try {
			if(conn==null || conn.isClosed()  ){
				 conn=  getConn();
			}
		    stmt = conn.createStatement();
			return stmt;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取预处理语句对象PreparedStatement
	 * @param sql
	 * @return
	 */
	public PreparedStatement  getPreparedStatement(String sql ){
		try {
			if(conn==null || conn.isClosed() ){ 
				 conn=  getConn();
			}
			pstmt = conn.prepareStatement(sql);
			return pstmt; 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**更新记录的方法:insert 、update 、delete
	 * @param sql
	 * @return
	 */
	public int  update(String sql){
		try {
			if(stmt==null || stmt.isClosed() ){
				getStatement();
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			int res = stmt.executeUpdate(sql);
			return res;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("error"+e.getMessage());
		}finally{
			close();
		}
		return 0;
	}
	//注意:JDBC 执行完sql之后,要释放资源:倒叙关闭
	/**
	 * 释放资源的工具方法
	 */
	public  void close(){
		if(resultSet !=null ){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
3.DButil

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC操作数据库的工具类
* @ClassName: DBUtil 
 */
public class DBUtil {
	String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	String userName="system";
	String password ="oracledba";
	private Connection conn=null;
	private Statement stmt=null;
	private PreparedStatement pstmt=null;
	private ResultSet resultSet =null;
	
	static{
		//第一步:加载驱动
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取连接的工具方法
	 * @return
	 */
	public  Connection   getConn(){
			try {
			 //第二步:获取连接
			    conn =DriverManager.getConnection(url, userName, password);
			    return conn;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return null;
	}
	
	/**
	 * 获取语句对象的工具方法
	 * @return
	 */
	public  Statement getStatement(){
		
		try {
			if(conn==null || conn.isClosed()  ){
				 conn=  getConn();
			}
		    stmt = conn.createStatement();
			return stmt;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取预处理语句对象PreparedStatement
	 * @param sql
	 * @return
	 */
	public PreparedStatement  getPreparedStatement(String sql ){
		try {
			if(conn==null || conn.isClosed() ){ 
				 conn=  getConn();
			}
			pstmt = conn.prepareStatement(sql);
			return pstmt; 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**更新记录的方法:insert 、update 、delete
	 * @param sql
	 * @return
	 */
	public int  update(String sql){
		try {
			if(stmt==null || stmt.isClosed() ){
				getStatement();
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			int res = stmt.executeUpdate(sql);
			return res;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("error"+e.getMessage());
		}finally{
			close();
		}
		return 0;
	}
	//注意:JDBC 执行完sql之后,要释放资源:倒叙关闭
	/**
	 * 释放资源的工具方法
	 */
	public  void close(){
		if(resultSet !=null ){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}



1.Socket服务端实现步骤

1.创建ServerSocket对象,绑定并监听端口

2.通过accept监听客户端的请求

3.建立连接后,通过输出输入流进行读写操作

4.将读取的信息存入数据库,并进行下一次的监听

你可能感兴趣的:(java,socket)