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

来源:互联网 发布:淘宝宝贝描述排版软件 编辑:程序博客网 时间:2024/06/05 23:58

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



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 blocke.printStackTrace();}}/** * 获取连接的工具方法 * @return */public  Connection   getConn(){try { //第二步:获取连接    conn =DriverManager.getConnection(url, userName, password);    return conn;} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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 blocke1.printStackTrace();}try {int res = stmt.executeUpdate(sql);return res;} catch (SQLException e) {// TODO Auto-generated catch blockSystem.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 blocke.printStackTrace();}}if(stmt!=null){try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(pstmt!=null){try {pstmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}}/** * 获取连接的工具方法 * @return */public  Connection   getConn(){try { //第二步:获取连接    conn =DriverManager.getConnection(url, userName, password);    return conn;} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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 blocke1.printStackTrace();}try {int res = stmt.executeUpdate(sql);return res;} catch (SQLException e) {// TODO Auto-generated catch blockSystem.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 blocke.printStackTrace();}}if(stmt!=null){try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(pstmt!=null){try {pstmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}



1.Socket服务端实现步骤

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

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

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

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

原创粉丝点击