JDBC的几种封装

1、不封装的写法

package jiahanglee;


import java.sql.*;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class Jdbc {
    public static void main(String []args)
    {
        Connection conn = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //建立数据库连接
            String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&&characterEncoding=UTF-8";
            conn = DriverManager.getConnection(url,"root","");
            //执行操作数据库
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Student ");
            while (rs.next()){
                System.out.print(rs.getString("name")+"\n");
            }
            } catch (SQLException e) {
                e.printStackTrace();
            }
         catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

2、一部份封装
connection1

package Utils;

import java.sql.*;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class Connection1 {
    public static Connection getConnection(){
      Connection conn = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //连接数据库
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";

                conn =  DriverManager.getConnection(url,"root","");

            } catch (SQLException e) {
            e.printStackTrace();
        }
         catch (ClassNotFoundException e){
            e.printStackTrace();
        }


        return conn;
    }
}

test

package jiahanglee;

import Utils.Connection1;
import java.sql.*;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class test {
    public static void main(String arg[]){
        Connection1 con = new Connection1();
        java.sql.Connection conn = (java.sql.Connection) con.getConnection();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Student");
            while(rs.next())
            {System.out.print(rs.getString(1));}
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}

3、connection2

package Utils;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class connection2 {
    private static Properties properties;

    static{

        InputStream is = connection2.class.getResourceAsStream("../resource/df.properties");
        properties = new Properties();

        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(properties.getProperty("dbDrive"));

                connection = DriverManager.getConnection(
                        properties.getProperty("dbURL"),
                        properties.getProperty("user"),
                        properties.getProperty("password"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
         catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return connection;
    }
}

test2

package jiahanglee;

import Utils.connection2;

import java.sql.*;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class test2 {
    public static void main(String arg[]){
        connection2 conr = new connection2();
        Connection conn = conr.getConnection();
        try {

            Statement stmp = conn.createStatement();
            ResultSet rs = stmp.executeQuery("SELECT * FROM Student");
            while(rs.next())
                System.out.print(rs.getString(1)+"\n");
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

3、完全封装
connection3

package Utils;

import en.Student;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class Connection3 {
connection2 cm = new connection2();
public List<Student> select(){
    List<Student> list = new ArrayList<Student>();
    Connection conn = cm.getConnection();
    try {
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("SELECT *FROM Student");
        while(rs.next())
        {
            Student s = new Student();
            s.setName(rs.getString("name"));
            list.add(s);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return  list;
}

}

test3

package jiahanglee;

import Utils.Connection3;
import en.Student;

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.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiahang Lee on 2017/5/8.
 */
public class test3 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> list;
        resp.setContentType("text/html;charset=utf-8");
        Connection3 c3 = new Connection3();

        list = c3.select();
        for(Student i:list)
        {
            resp.setContentType("text/html");
            resp.setCharacterEncoding("utf-8");
            PrintWriter writer=resp.getWriter();
            writer.println(i.getName().toString());
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Connection3 c3 = new Connection3();
        List<Student> list ;
        list = c3.select();
        for(Student i:list)
        {
            resp.setContentType("text/html");
            resp.setCharacterEncoding("utf-8");
            PrintWriter writer=resp.getWriter();
            writer.println(i.getName().toString());
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值