SttementとPreparedSttementの違い


[size=medium]PreparedSttementはSttementから引き継がれます。同构sql:sql文は基本的に同じですが、具体的なパラメータの数値が违います。異性sql:sql文は全然違います。Sttement不足:1.効率が低い2.フィールドタイプのサポートが悪い3.文法的な意味がはっきりしない(構造がよく分かりません)。コンパイルにはパラメータが必要ではないので、PreparedSttementはsql文の中のいくつかのパラメータの代わりに「?」を使ってもいいです。パラメータを持たないsql文を先にデータベースに送り、プリコンパイルしてから、PreparedSttementは設定されたパラメータをデータベースに送ります。PreparedSttementを使って対応するパラメータを設定する場合は、パラメータの位置とタイプを指定し、パラメータの具体的な値を与え、異なるパラメータタイプに応じて異なるsetXXX(パラメータの位置、パラメータ値)を使用してパラメータを設定します。String sql="udate student set name=?where id=4;」///その中のここではプレースホルダを表します。具体的な値は設定されていません。PreparedSttement pstm=con.preparement(sql);sql文はすでにデータベースに送信されてコンパイルされました。pstm.setXXX(パラメータの位置、パラメータの値)/パラメータ値をPreparedSttementオブジェクトに格納します。Pstm.executeUpdate()//事前に翻訳したので、sql文を再入力する必要がなく、そのまま実行できます。[/size]
Statement    :
package com.ambow.day19.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.ambow.day19.jdbc.util.JDBCConAndClo;
// :                   addBatch,executeBatch;
public class JDBCStatementTest {
   public static void main(String args[]){
		Connection con = null;
		Statement stm = null;
		ResultSet rs = null;
		try {
		    //1.  JDBC        
			con=JDBCConAndClo.getConnectionBao();
			System.out.println("con="+con);
//			//* Statement        :
	//         String sql1="insert into student values(12,'wang','java',55)";
	//	String sql2="insert into student values(13,'wang','java',95)";
	//	String sql3="insert into student values(14,'wadedng','java',45)";
//			stm = con.createStatement();
//			stm.executeUpdate(sql1);
//			stm.executeUpdate(sql2);
//			stm.executeUpdate(sql3);
//			System.out.println("    !");
			//* Statement         :
			String sql11="delete from student where id=1";
			String sql12="delete from student where id=2";
			String sql13="delete from student where id=3";
			stm = con.createStatement();
			stm.executeUpdate(sql11);
			stm.executeUpdate(sql12);
			stm.executeUpdate(sql13);
			System.out.println("    !");
			//* Statement        :
			//2.   sql  :
			String sql = "select * from student";
			//     statement(  sql)
			stm = con.createStatement();
			//     sql  
			rs = stm.executeQuery(sql);
			// 3.  sql   :
			while(rs.next()){
				System.out.print(rs.getString("id")+" ");
				System.out.print(rs.getString("name")+" ");
				System.out.print(rs.getString("course")+" ");
				System.out.println(rs.getString("score"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//4.     ,     :
			JDBCConAndClo.closeResultSet(rs);
			JDBCConAndClo.closeStatement(stm);
			JDBCConAndClo.closeConnection(con);
		}
   }
} 
PreparedStatement    :

package com.ambow.day19.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ambow.day19.jdbc.util.JDBCConAndClo;
// :                   addBatch,executeBatch;
public class JDBCPreparedStatementTest {
  public static void main(String args[]){
	  Connection con=null;
	  PreparedStatement pstm=null;
	  ResultSet rs=null;
	  try {
		con=JDBCConAndClo.getConnectionBao();
		//* PreparedStatement         ;
		//String sql="insert into student values(10,'  ','  ',90)";
		String sql="insert into student values(?,?,?,?)";
		//1.   PreparedStatement  (  slq  ):
		pstm=con.prepareStatement(sql);
		//2.   sql  :
		pstm.setInt(1,11);
		pstm.setString(2,"wangqinqin");
		pstm.setString(3, "hibernate");
		pstm.setInt(4, 85);
		//3.   sql  :
	    pstm.executeUpdate();
	    System.out.println("    !");
	   
	    //* PreparedStatement         ;
	    String sql2="delete from student where id=?";
	    pstm=con.prepareStatement(sql2);
	    pstm.setInt(1,5);
	    pstm.executeUpdate();
	    System.out.println("    !");
	  
	    //* PreparedStatement          ;
	    String sql1="select * from student where id=?";
	    pstm=con.prepareStatement(sql1);
	    pstm.setInt(1,8);
	    rs=pstm.executeQuery();
	    System.out.println("     :");
	    //      ;
		while(rs.next()){
			System.out.print(rs.getString("id")+" ");
			System.out.print(rs.getString("name")+" ");
			System.out.print(rs.getString("course")+" ");
			System.out.println(rs.getString("score"));
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		JDBCConAndClo.closeResultSet(rs);
		JDBCConAndClo.closePreparedStatement(pstm);
		JDBCConAndClo.closeConnection(con);
	}
	  
	  }
}
                   JDBCConAndClo  :
package com.ambow.day19.jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCConAndClo {
	public static void main(String args[]) {
		JDBCConAndClo jc = new JDBCConAndClo();
		jc.getConnectionBao();
	}
   //  JDBC          ;
	public static Connection getConnectionBao() {
		Connection con = null;
		String URL = "jdbc:oracle:thin:@localhost:1521:ambow";
		String user = "system";
		String password = "wqq123";
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection(URL, user, password);
			if (!con.isClosed()) {
				System.out.println("       !");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("con=" + con);
		return con;
	}
   //  ResultSet
	public static void closeResultSet(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	//  Statement
	public static void closeStatement(Statement stm) {
		if (stm != null) {
			try {
				stm.close();
				stm = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	//  PreparedStatement
	public static void closePreparedStatement(PreparedStatement pstm) {
		if (pstm != null) {
			try {
				pstm.close();
				pstm = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	//  Connection
	public static void closeConnection(Connection con) {
		if (con != null) {
			try {
				con.close();
				con = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			con = null;
		}
	}
}