JDBC知識學習——Statement執行更新和ResultSet查詢操作

Statement用於執行sql語句的對象:

1.通過Connection的createStatement()方法來獲取
2.通過executeUpdate(sql)執行sql語句,可以執行insert,update,delete
代碼示例:

public class StatementTest {
    /*
    * ResultSet結果集封裝了使用JDBC查詢得到的結果
    * 1.調用Statement對象的executeQuery(sql)得到結果集
    * 2.ResultSet返回的是一張數據表
    * */
    @Test
    public void testResultSet() throws Exception{
        //獲取指定id的數據並打印
        //1.獲取連接
        Connection connection=getConnection();
        //2.獲取statement
        Statement statement=connection.createStatement();
        //3.準備sql
        String sql="select id,name,pwd,sex,home,info from user where id=8";
        String sql1="select id,name,pwd,sex,home,info from user " ;//查詢多條數據
        //4.執行查詢
        ResultSet re = statement.executeQuery(sql1);
        //5.處理得到的ResultSet
    while(re.next()){//if(re.next()){
        int id=re.getInt(1);
        String name=re.getString("name");
        String sex=re.getString(4);
        String info=re.getString(5);
        System.out.println(id+"\t"+name+"\t"+sex+"\t"+info);
    }
        //6.關閉數據庫資源
        re.close();
        JDBCTools.releaseSource(statement,connection);
    }

    @Test
    public void test1() {
        Connection con = null;
        Statement statement = null;
        try {
            //獲取數據庫連接
            con = getConnection();
            //準備插入的sql語句
            String sql = "insert into user(name,pwd,sex,home,info)" +
                    "values('Roy','111','Man','Lz','sky');";
            String sql1 = "delete from user where id=9;";
            String sql2 = "update user set name='Jerry' where id=6;";

            //執行插入操作
            //1.獲取執行sql語句的Statement對象
            statement = con.createStatement();
            //2調用方法進行插入
            statement.executeUpdate(sql2);
            System.out.println("sql執行成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTools.releaseSource(statement,con);
            /*if (statement != null)
            //關閉Statement對象
            {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null)
            //關閉連接
            {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }*/

        }
    }
    /**
    *@ClassName StatementTest
    *@Description 獲取數據庫資源連接
    *@Param []
    *@Return java.sql.Connection
    *@Date 2020/2/13 15:57
    *@Author Roy
    */
    public Connection getConnection() throws Exception {
        //連接數據庫的字符串
        String driverClass = null;//驅動的全類名
        String jdbcUrl = null;
        String user = null;
        String password = null;
        //讀取類路徑下的jdbc.properties文件
        InputStream in =
                this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
        driverClass = properties.getProperty("driver");
        jdbcUrl = properties.getProperty("jdbcUrl");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
        //加載數據庫驅動程序
        Class.forName(driverClass);
        //獲取數據庫連接
        Connection connection1 = DriverManager.getConnection(jdbcUrl, user, password);
        return connection1;
    }
}

操作JDBC的工具類封裝了工具方法

代碼示例:

public class JDBCTools {
    /**
    *@ClassName JDBCTools
    *@Description 讀取連接的方法通過讀取配置文件獲取數據庫服務器連接
    *@Param []
    *@Return java.sql.Connection
    *@Date 2020/2/13 15:51
    *@Author Roy
    */
    public static Connection getConnection() throws  Exception{
        String driverClass=null;
        String jdbcUrl=null;
        String user=null;
        String password=null;
        //讀取類路徑下的jdbc.properties文件
        InputStream in=
                JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties=new Properties();
        properties.load(in);
        driverClass=properties.getProperty("driver");
        jdbcUrl=properties.getProperty("jdbcUrl");
        user=properties.getProperty("user");
        password=properties.getProperty("password");
        //通過反射創建對象
        Driver driver1=(Driver) Class.forName(driverClass).newInstance();
        Properties inf=new Properties();
        inf.put("user",user);
        inf.put("password",password);
        //獲取數據庫連接
        Connection connection1=driver1.connect(jdbcUrl,inf);
        return connection1;
    }
    /**
    *@ClassName JDBCTools
    *@Description 釋放資源,關閉statement和Connection
    *@Param [statement, connection]
    *@Return void
    *@Date 2020/2/13 15:54
    *@Author Roy
    */

    public static void releaseSource(Statement statement,Connection connection){
        if (statement != null)
        //關閉Statement對象
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null)
        //關閉連接
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章