最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢操作

 更新時間:2022年09月19日 15:00:47   作者:夏志121  
這篇文章主要為大家詳細(xì)介紹了Java如何使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

一、使用PrepareStatement實(shí)現(xiàn)插入數(shù)據(jù)的操作

public class PreparedStatementUpdateTest {
    @Test
    public void testInsert() {
        Connection connection = null;
        PreparedStatement ps = null;
 
        try {
            //1.獲取鏈接
            connection = JDBCUtils.getConnection();
            //2.預(yù)編譯sql語句,返回PreparedStatement實(shí)例
            String sql = "insert into customers(name,email,birth)
            value(?,?,?)";
            ps = connection.prepareStatement(sql);
            //3.填充占位符
            ps.setString(1,"哪吒");
            ps.setString(2,"nezha@gmail.com");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date parse = sdf.parse("2001-03-21");
            long time = parse.getTime();
            //這個Date是sql中的Date
            ps.setDate(3,new Date(time));
            //4.執(zhí)行操作
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //5.資源釋放
        JDBCUtils.closeConnection(connection,ps);
    }
}

二、使用PrepareStatement實(shí)現(xiàn)查詢數(shù)據(jù)的操作

public class CustomerForQuery {
    public Customer commonQueryForCustomer(String sql,Object ...obj){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //1.獲取連接
            conn = JDBCUtils.getConnection();
            //2.預(yù)編譯sql語句,返回PreparedStatement實(shí)例
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i+1,obj[i]);
            }
            //4.執(zhí)行查詢操作
            resultSet = ps.executeQuery();
            //5.獲取描述結(jié)果集的對象
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            //6.處理結(jié)果集
            if(resultSet.next()){
                Customer customer = new Customer();
                for (int i = 0; i < columnCount; i++) {
                    Object object = resultSet.getObject(i + 1);
                    String columnName = metaData.getColumnName(i + 1);
                    Field declaredField =
                            Customer.class.getDeclaredField(columnName);
                    declaredField.setAccessible(true);
                    declaredField.set(customer,object);
                }
                return customer;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.關(guān)閉資源
            JDBCUtils.closeConnection(conn,ps,resultSet);
        }
        return null;
    }
    @Test
    public void test(){
        String sql = "select name,email from customers where id=?";
        Customer customer = commonQueryForCustomer(sql, 2);
        System.out.println(customer);
    }
}

用到的bean類:

public class Customer {
    private int id;
    private String name;
    private String email;
    private Date birth;
 
    public Customer() {
    }
 
    public Customer(int id, String name, String email, Date birth) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.birth = birth;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public Date getBirth() {
        return birth;
    }
 
    public void setBirth(Date birth) {
        this.birth = birth;
    }
 
    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", birth=" + birth +
                '}';
    }
}

三、ResultSet和ResultSetMetaData

ResultSet:

1.查詢需要調(diào)用PreparedStatement 的 executeQuery() 方法,查詢結(jié)果是一個ResultSet 對象

2.ResultSet 對象以邏輯表格的形式封裝了執(zhí)行數(shù)據(jù)庫操作的結(jié)果集,ResultSet 接口由數(shù)據(jù)庫廠商提供實(shí)現(xiàn)

3.ResultSet 返回的實(shí)際上就是一張數(shù)據(jù)表。有一個指針指向數(shù)據(jù)表的第一條記錄的前面ResultSet 對象維護(hù)了一個指向當(dāng)前數(shù)據(jù)行的游標(biāo),初始的時候,游標(biāo)在第一行之前,可以通過 ResultSet 對象 的 next() 方法移動到下一行。調(diào)用 next()方法檢測下一行是否有效。若有效,該方法返回 true,且指針下移。 相當(dāng)于Iterator對象的 hasNext() 和 next() 方法的結(jié)合體。 當(dāng)指針指向一行時, 可以通過調(diào)用 getXxx(int index) 或 getXxx(int columnName) 獲取每一列的值。 例如: getInt(1), getString("name")

注意:Java與數(shù)據(jù)庫交互涉及到的相關(guān)Java API中的索引都從1開始

ResultSetMetaData:

1.可用于獲取關(guān)于ResultSet對象中列的類型和屬性信息的對象

2.通過ResultSet的getMetaData()方法獲取

3.常用方法

  • getColumnName(int column)方法,獲取指定列的名稱
  • getColumnLabel(int column)方法,獲取指定列的別名,如果不指定別名則返回名稱
  • getColumnCount()方法,返回當(dāng)前 ResultSet 對象中的列數(shù)

四、資源釋放

釋放ResultSet, Statement,Connection。

數(shù)據(jù)庫連接(Connection)是非常稀有的資源,用完后必須馬上釋放,如果Connection不能及時正確的關(guān)閉將導(dǎo)致系統(tǒng)宕機(jī)

Connection的使用原則是盡量晚創(chuàng)建,盡量早的釋放。 可以在finally中關(guān)閉,保證及時其他代碼出現(xiàn)異常,資源也一定能被關(guān)閉。

到此這篇關(guān)于Java使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢操作的文章就介紹到這了,更多相關(guān)Java PrepareStatement內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

县级市| 乌拉特前旗| 屏南县| 承德市| 三门峡市| 赞皇县| 吉安县| 延吉市| 兴仁县| 深水埗区| 宽甸| 平阴县| 江安县| 蓝田县| 株洲市| 阳西县| 徐闻县| 云安县| 铁岭县| 唐海县| 凌海市| 肃北| 茂名市| 临沂市| 潜山县| 谷城县| 武夷山市| 亳州市| 北票市| 航空| 青铜峡市| 昌邑市| 怀宁县| 和田县| 随州市| 新巴尔虎右旗| 缙云县| 满城县| 贞丰县| 蒙阴县| 府谷县|