JDBC_PreparedSttementを使ってudate操作を実行します.

6939 ワード

1.PreparedSttementを使用する
1)Sttementを使うにはSQL文をスペルする必要があります.大変で間違いやすいです.
PreparedSttementはSttementのサブインターフェースであり、プレースホルダを持つsql文に入ることができ、補足プレースホルダ変数の方法を提供します.
2)PreparedSttementステップを使用する
a.PreparedSttementを作成する:
String sql=“”insert into student valuse(?????、?)”PreparedSttement ps=conn.preparedSttement(sql);
b.PreparedSttementのsetXXX(int index、Object obj)を呼び出します.プレースホルダの値を設定します.インデックス値indexは1から始まります.
c.sql文を実行する:executeQuery()またはexecuteUpdate()は、実行時にsql文に入らないように注意してください.
例コードは以下の通りです.JDBCTools.java
package com.atchance.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/** * JDBCTools     : * 1,        * 2.            * 3.           * 1)   release   :              * @author chance * */
public class JDBCTools {

    public static void release(ResultSet rs,Statement statement, Connection con) throws SQLException{
        /** *   ResultSet Statement Connection        */
        try {
            if(rs != null)
                rs.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            if(statement != null)
                statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(con != null)
                con.close();
        }
    }

    public static void release(Statement statement, Connection con) throws SQLException{
        /** * Statement Connection        */
        try {
            if(statement != null)
                statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(con != null)
                con.close();
        }
    }
    public static Connection getConnection() throws Exception{
            /* *          * */
            String driverClass = "oracle.jdbc.driver.OracleDriver";
            String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
            String user = "scott";
            String password = "tiger";

            Driver driver = (Driver)Class.forName(driverClass).newInstance();

            Properties info = new Properties();
            info.put("user", user);
            info.put("password", password);
            Connection connection = driver.connect(jdbcUrl,info);
            return connection;
        }
    public void update_1() throws Exception{
        /** *   update      PreparedStatement            sql      */
        Connection con = null;
        PreparedStatement ps = null;
        try {
            //    
            con = JDBCTools.getConnection();
            //  sql  
            String sql = "insert into student values(?,?,?,?)";
            ps = con.prepareStatement(sql);
            //  PreparedStatement setXXX(int index, Object obj);
            ps.setString(1, "90010");
            ps.setString(2, "  ");
            ps.setString(3, " ");
            ps.setString(4, "  ");
            //  sql  
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //       
            JDBCTools.release(null, ps, con);
        }
    }
}
2.PreparedSttementを使うメリット
1)コードの可読性と維持性2)PreparedStatimentは、できるだけ性能を向上させることが重要である.3)最も重要な点は、安全性すなわちsql注入を大幅に向上させたことである.この点については、他のブログでは、万能パスワードを利用してサイトのバックグラウンド原理及び脆弱性修復方法PreparedStitmentを登録してSQL注入を防ぐことができる.例えば:String sql="SELECT*FROM user WHERE name="+userName+"and password="+passWord+";悪意の入力パラメータ値:userName="1"OR"="1"passWord=「1」OR「1」="1"最終的にsqlは、Stering sql="SELECT*FROM user WHERE name="1"OR"='1'and password="1"="1"="1"""";となります.この文はselect*from userに相当します.もしユーザーにdrop table userを加えると、結果は想像に耐えないです.したがって、PreparedSttementを使用すると、データベースシステムはパラメータの内容をSQLコマンドの一部として扱わないので、SQLコマンドのコンパイルがデータベースで完了した後にのみ、パラメータを使用して実行することができます.したがって、パラメータに破壊的なコマンドが含まれていても、データベースによっては実行されません.4)同じ作業量でのデータ効率が高い