JDBC1:Statement和PreparedStatement

JDBC

1.什麼是ORM

ORM:在Java對象和關係數據庫之間創建某種映射,就能夠實現直接存取Java對象,JavaBeanmysql

2.什麼是JDBC

JDBC:Java Data Base Connectivity 是一個接口,能夠執行SQL語句的JavaAPI程序員

3.爲何要用JDBC

1.數據庫中的數據是給誰用的sql

數據是給程序用的,而咱們寫的就是Java的程序,因此要用Java程序去鏈接數據庫來訪問數據數據庫

2.世面上有不少數據庫,原本咱們是須要根據不一樣的數據庫學習不一樣的API,sun公司爲了簡化這個操做,提高程序員的幸福感,就定義了一套規範,就是JDBC API(接口)。對咱們來講,使用不一樣的數據庫時,只要用數據庫廠商提供的數據庫驅動程序便可。ide

4. 怎麼作呢

步驟:
(1)導入MySQL的驅動包學習

(2)裝載數據庫驅動程序url

(3)獲取到與數據庫的鏈接code

(4)獲取能夠執行的SQL語句的對象對象

(5)執行SQL語句接口

(6)獲取結果集--不是必須有,看需求

(7)關閉鏈接

4.1 第一種

使用Statement做爲執行SQL語句的對象:

@Test
       public void getConnection1(){
           //
           Connection connection=null;
           Statement statement=null;
           ResultSet resultSet=null;

           try{
               //1.加載驅動
               //能夠省略註冊驅動DriverManager.registrDriver(new com.mysql.jdbc.Driver);
               //是由於Driver類裏靜態加載了註冊驅動的步驟
//               static {
//                   try {
//                       DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//                   } catch (SQLException var1) {
//                       throw new RuntimeException("Can't register driver!");
//                   }
//               }
               Class.forName("com.mysql.jdbc.Driver");
               //2.獲取數據庫鏈接
               String url="jdbc:mysql://localhost:3306/myemployees?useSSL=false";
               String user="root";
               String password="419423mmzz";
               connection = DriverManager.getConnection(url, user, password);
               System.out.println(connection);
               //3.獲取能夠執行sql語句的對象Statement
               //Statement的弊端:(1)須要拼寫sql語句(2)存在SQL注入問題
               //如何讓避免出現SQL注入:用PreparedStatement(從Statement擴展而來),取代Statement
               statement=connection.createStatement();
               //4.獲取結果集
               String sql="SELECT * FROM jobs";
               resultSet=statement.executeQuery(sql);
               //5.處理結果集
               while (resultSet.next()) {
                   String job_id=resultSet.getString(1);
                   String job_title=resultSet.getString(2);
                   String max_salary=resultSet.getString(3);
                   String min_salary = resultSet.getString(4);
                   System.out.println(job_id+","+job_title+" "+min_salary+" "+max_salary);
               }
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           } catch (SQLException e) {
               e.printStackTrace();
           }finally {
               //6.關閉鏈接
               if (resultSet != null) {
                   try {
                       resultSet.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
               if (statement != null) {
                   try {
                       statement.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
               if (connection != null) {
                   try {
                       connection.close();
                   } catch (SQLException e) {
                       e.printStackTrace();
                   }
               }
           }
       }
4.2 使用PreparedStatement替換Statement

1、爲何要進行替換呢

一、Statement對象編譯SQL語句時,若是SQL語句有變量,就須要使用分隔符來隔開。若是變量不少,就會使得SQL變得很是複雜。PreparedStatement可使用佔位符,簡化SQL的編寫。

2.Statement會頻繁編譯SQL.PreparedStatement可對SQL進行預編譯,提升效率,預編譯的SQL存儲在PreparedStatement對象中。

3.PreparedStatement防止SQL注入。(Statement經過分隔符‘++’,編寫永等式,能夠不須要密碼就進入數據庫)

public class Jdbc2 {
    @Test
    public void testInsert(){
        Connection connection=null;
        PreparedStatement ps=null;

        /**
        *這裏的第一步,是JDBC鏈接數據庫的第二種方式,經過讀取配置文件jdbc.properties中的信息
        *來獲取數據庫鏈接必須的四個數據
        */
        //1.讀取配置文件的信息
        InputStream is                  =Jdbc2.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros=new Properties();
        try {
            pros.load(is);
            String driver = pros.getProperty("driver");
            String url = pros.getProperty("url");
            String user = pros.getProperty("user");
            String password = pros.getProperty("password");

            //2.加載驅動
            Class.forName(driver);
            //3.獲取數據庫的鏈接
           connection = DriverManager.getConnection(url, user, password);
            //4.獲取執行SQL語句的對象PrepareStatement
            //預編譯sql語句
            String sql="insert into jobs(job_id,job_title,min_salary,max_salary) values(?,?,?,?)";
            ps = connection.prepareStatement(sql);
            //5.填充佔位符
            ps.setString(1, "ZM");
            ps.setString(2, "IT");
            ps.setInt(3, 10000);
            ps.setInt(4,30000);
            //6.執行操做
            ps.execute();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.關閉資源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

jdbc.properties配置文件中的信息

//jdbc.properties配置文件中的信息
user=root
password=123
url=jdbc:mysql://localhost:3306/myemployees?useSSL=false
driver=com.mysql.jdbc.Driver
相關文章
相關標籤/搜索