程序员

连接数据库—初识JavaBean

2018-06-04  本文已影响12人  Allen的光影天地

我们来看一个小例子:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class ShowRs extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/bbs?characterEncoding=utf8&useSSL=false";
            connection = DriverManager.getConnection(url, "root", "332999");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from article");
            while (resultSet.next()){
                out.println(resultSet.getString("title"));
                out.println("\n");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

                try {
                    if (resultSet != null)
                        resultSet.close();
                    if (statement != null)
                        statement.close();
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
    }
}


  1. db.java
import com.sun.jdi.event.StepEvent;

import java.sql.*;

// 在servlet中使用javabean就是在使用封装数据库的类
public class DB {
    public static Connection getConn() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/bbs?characterEncoding=utf8&useSSL=false";
            connection = DriverManager.getConnection(url, "root", "332999");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static Statement getStatement(Connection connection){
        Statement statement = null;
        try {
            if (connection != null){
                statement = connection.createStatement();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return statement;
    }
    public static ResultSet getResultSet(Connection connection,String sql){
        Statement statement = null;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet resultSet  = getResultSet(statement, sql);
        close(statement);
        return resultSet;
    }

    public static ResultSet getResultSet(Statement statement,String sql){
        ResultSet resultSet = null;
        try {
            if (statement != null){
                resultSet = statement.executeQuery(sql);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    public static void close(Connection connection){
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Statement statement){
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet resultSet){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

  1. ShowRsUseBean.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.desktop.SystemSleepEvent;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ShowRsUseBean extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("charset=utf-8");
        PrintWriter out = resp.getWriter();
        String sql = "select * from article";
        Connection connection = DB.getConn();
        Statement statement = DB.getStatement(connection);
        ResultSet resultSet = DB.getResultSet(statement,sql);
        try {
            while (resultSet.next())
            {
                out.println(resultSet.getString("title"));
                System.out.println(resultSet.getString("title"));
                out.println("\n\n\r");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DB.close(resultSet);
            DB.close(statement);
            DB.close(connection);
        }

    }
}
上一篇下一篇

猜你喜欢

热点阅读