JDBC中prepareStatement 和Statement 的区别

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(); 

    }
}

  

上一篇:iOS开发中几个重要的方法


下一篇:GForms开发平台