久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Java PreparedStatement用法詳解

瀏覽:126日期:2023-02-11 13:44:56
PreparedStatement介紹 可以通過調用 Connection 對象的 prepareStatement(String sql) 方法獲取 PreparedStatement 對象PreparedStatement 接口是 Statement 的子接口,它表示一條預編譯過的 SQL 語句 PreparedStatement 對象所代表的 SQL 語句中的參數用問號(?)來表示(?在SQL中表示占位符),調用 PreparedStatement 對象的 setXxx() 方法來設置這些參數. setXxx() 方法有兩個參數,第一個參數是要設置的 SQL 語句中的參數的索引(從 1 開始),第二個是設置的 SQL 語句中的參數的值

Java PreparedStatement用法詳解

PreparedStatement vs Statement 代碼的可讀性和可維護性。 PreparedStatement 能最大可能提高性能: DBServer會對預編譯語句提供性能優化。因為預編譯語句有可能被重復調用,所以語句在被DBServer的編譯器編譯后的執行代碼被緩存下來,那么下次調用時只要是相同的預編譯語句就不需要編譯,只要將參數直接傳入編譯過的語句執行代碼中就會得到執行。在statement語句中,即使是相同操作但因為數據內容不一樣,所以整個語句本身不能匹配,沒有緩存語句的意義.事實是沒有數據庫會對普通語句編譯后的執行代碼緩存。這樣每執行一次都要對傳入的語句編譯一次。(語法檢查,語義檢查,翻譯成二進制命令,緩存) PreparedStatement 可以防止 SQL 注入插入案例

PreparedStatement常用的方法:

void setObject(int parameterIndex, Object x, int targetSqlType)

Java PreparedStatement用法詳解

parameterIndex the first parameter is 1, the second is 2, …占位符參數索引是從1開始的其余也是如此:

void setInt(int parameterIndex, int x)void setLong(int parameterIndex, long x)void setString(int parameterIndex, String x)void setBlob (int parameterIndex, Blob x)void setDate(int parameterIndex, java.sql.Date x, Calendar cal)

Java PreparedStatement用法詳解

執行操作:

Java PreparedStatement用法詳解

package com.atmf;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Properties;import org.junit.Test;public class SumUP {@Testpublic void getConnection() {Connection con = null;PreparedStatement ps = null;try {//1,加載配置文件InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream('jdbc.properties');Properties pr = new Properties();pr.load(is);//2,讀取配置信息String user = pr.getProperty('user');String password = pr.getProperty('password');String url = pr.getProperty('url');String driverClass = pr.getProperty('driverClass');//3.加載驅動Class.forName(driverClass);//4,獲取連接con = DriverManager.getConnection(url, user,password);String sql = 'insert into customers(name,birth) value(?,?)';//預編譯sql語句,得到PreparedStatement對象ps = con.prepareStatement(sql);//5,填充占位符ps.setString(1, '三明治');SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd');Date date = sdf.parse('2020-11-02');ps.setDate(2, new java.sql.Date(date.getTime()));//6,執行操作ps.execute();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {//7,關閉資源try {if(ps != null)ps.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(con != null)con.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

配置信息:jdbc.properties文件user=rootpassword=123456url=jdbc:mysql://localhost:3306/studentsdriverClass=com.mysql.jdbc.Driver

執行結果:

Java PreparedStatement用法詳解

PreparedStatement實現對表數據的增刪改查操作

到此這篇關于Java PreparedStatement用法詳解的文章就介紹到這了,更多相關Java PreparedStatement用法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 云霄县| 华池县| 岳阳市| 响水县| 明星| 永春县| 南安市| 岳普湖县| 沧州市| 深州市| 陕西省| 航空| 桂林市| 望江县| 咸阳市| 怀远县| 淳化县| 万全县| 余干县| 河池市| 和硕县| 南皮县| 东台市| 阿荣旗| 石台县| 仙游县| 泰宁县| 金堂县| 陕西省| 平远县| 东方市| 高安市| 鞍山市| 集贤县| 高清| 崇阳县| 濉溪县| 林芝县| 安乡县| 馆陶县| 常宁市|