日期:2014-05-16 浏览次数:20447 次
package com.JDBC.proc; import java.sql.*; public class StatementTest { public static void main(String args[]){ Connection conn=null; Statement stm=null; ResultSet rs=null; try { conn=DBTool.getConnection(); String sql="select EmpNo,EName from emp " + "where empNo=7499"; stm=conn.createStatement(); rs=stm.executeQuery(sql); while(rs.next()){ System.out.println(rs.getInt(1)+"---"+rs.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally{ DBTool.release(rs, stm, conn); } } }
package com.JDBC.proc; import java.sql.*; public class PrepareStatement { public static void main(String[] args){ Connection conn=null; PreparedStatement psmt=null; ResultSet rs=null; try { conn=DBTool.getConnection(); String sql="select EmpNo,Ename " + "from emp " + "where EmpNo=?"; psmt=conn.prepareStatement(sql); psmt.setInt(1, 7499); rs=psmt.executeQuery(); while(rs.next()){ System.out.println(rs.getInt(1)+"---"+rs.getString(2)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally{ DBTool.release(rs, psmt, conn); } } }[/size]