JDBC中prepareStatement 和Statement 的区别

来源:互联网 发布:基站定位数据库 编辑:程序博客网 时间:2024/05/02 00:08
package 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 DButil { 
    public static void main(String[] args) throws SQLException { 
        try 
        { 
            Class.forName("oracle.jdbc.driver.OracleDriver");//加载并注册驱动程序 
        }catch(ClassNotFoundException e)//加载错误,捕获异常 
        { 
            System.out.println("加载驱动失败"); 
        } 
          Connection con =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","tiger"); //创建Connection连接对象
           PreparedStatement sta=con.prepareStatement("select * from emp");
           ResultSet rs=sta.executeQuery();
           /* Statement sta = con.createStatement(); //创建语句对象 
           ResultSet rs=sta.executeQuery("select * from emp"); //4,执行语句对象,如果查询,要把查询结果 放到结果集当中   */
            while(rs.next()){ //当没有到结尾的时候 
                System.out.print(rs.getInt("empno")+" ");//用Get方法获取字段的值 
                System.out.print(rs.getString("ename")+" "); 
                System.out.print(rs.getDouble("sal")); 
                System.out.println(); 
            } 
            //5,关闭资源 
            rs.close(); 
            sta.close(); 
            con.close(); 
            
    } 

}


PreparedStatement 在性能上远远超过了 Statement.....



0 0
原创粉丝点击