JavaWeb学习笔记之数据库连接

JDBC MySql 数据库连接

公共类 ConnetionFactory 代码如下

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

public class ConnectionFactoryTest {
    private final String dbDriver = "com.mysql.jdbc.Driver";
    private final String url = "jdbc:mysql://localhost:3306/xmu_cs";
    private final String userName = "root";
    private final String password ="root";

    private ResultSet rs = null;
    private Statement stmt = null;
    private Connection conn = null;

    //创建加载数据库驱动的方法
    public ConnectionFactoryTest(){
        try {
            Class.forName(dbDriver).newInstance();//系统自动加载数据库驱动
            System.out.println("数据库驱动加载成功!");
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("数据库加载失败");
        }
    }
    //创建数据库连接的方法
    public boolean createConnection(){
        try {
            conn = (Connection) DriverManager.getConnection(url, userName, password);
            conn.setAutoCommit(true);
            System.out.println("数据库连接成功!");
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
            System.out.println("数据库连接失败!");
            return false;
        }
    }

    //创建查询语句的方法
    public ResultSet executeQuery(String sql){
        try {
            if(conn == null){
                createConnection();
            }
            stmt = conn.createStatement();
            try {
                rs = stmt.executeQuery(sql);
                return rs;
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println(e.getMessage());
                return null;
            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
            return null;
        }
    }

    //创建执行更新操作的方法
    public boolean executeUpdate(String sql){
        if(conn == null){
            createConnection();
        }
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
            return false;
        }
    }

    //创建关闭数据库连接的方法
    public void closeConnection(){
        if(null != rs){
            try {
                rs.close();
                System.out.println("结果集关闭成功!");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(null != stmt){
            try {
                stmt.close();
                System.out.println("Statement关闭成功!!");
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        if(null !=conn){
            try{
                conn.close();
                System.out.println("数据库连接关闭成功!");
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}

编写jUnit测试类,代码如下:

public class dbConnectionTest {
    private ResultSet rs = null;
    private static ConnectionFactoryTest cf = new ConnectionFactoryTest();
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        cf.createConnection();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        cf.closeConnection();
    }

    @Test
    public void testQuery() throws SQLException {
        String sql = "select * from test";
        rs = cf.executeQuery(sql);
        while(rs.next()){
            System.out.println("id:"+rs.getString("id")+" | name:"+rs.getString("name"));
        }

    }

}

结果如下:

数据库驱动加载成功!
数据库连接成功!
id:1 | name:test1
id:2 | name:test2
结果集关闭成功!
Statement关闭成功!!
数据库连接关闭成功!

你可能感兴趣的:(java,mysql,Web,数据库)