jdbc之preparedStatement运用实例
来源:互联网 发布:sql查2个字段重复数据 编辑:程序博客网 时间:2024/06/06 03:39
preparedStatement比statement的好处:
1.预编译,前提是数据库支持预编译,如果数据库不支持,就和statement效果一样
2.动态传参
package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;public class PreparedStatement {/** * @Title: main * @Description: * @param: * @return void * @user: wangzg * @Date:2014-7-1 * @throws */public static void main(String[] args) {// TODO Auto-generated method stubConnection conn = null;java.sql.PreparedStatement pstm = null;ResultSet rs = null;try {conn = getConnection();//预编译,动态传参,效率比statement高pstm = conn.prepareStatement("select * from t_user where user_name = ?");pstm.setString(1, "wzg");rs = pstm.executeQuery();while(rs.next()){System.out.println(rs.getString(1));System.out.println(rs.getString("USER_NAME"));}pstm = conn.prepareStatement("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,?,?)");pstm.setString(1, "wzg");pstm.setString(2, "wzg");pstm.executeUpdate();rs.close();pstm.close();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/** * * @Title: getConnection * @Description: * @param: * @return Connection * @user: wangzg * @Date:2014-7-7 * @throws */public static Connection getConnection() throws ClassNotFoundException, SQLException{Connection conn = null;//加载oracle驱动Class.forName("oracle.jdbc.driver.OracleDriver");//连接数据库String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";String user ="wzg";String password = "wzg";conn = DriverManager.getConnection(url, user, password);return conn;}}
0 0
- jdbc之preparedStatement运用实例
- JDBC之addBatch运用实例
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC 之 PreparedStatement 接口
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC之PreparedStatement
- Java JDBC 之 PreparedStatement
- JDBC之PreparedStatement
- JDBC之PreparedStatement
- JDBC之preparedStatement
- JDBC之PreparedStatement接口
- 如何为thunderbird编写插件
- 网站跨域访问解决方法
- android开发之定制标题栏 --- 附源码
- 大端小端存储模式
- 孙卫琴的书籍不错
- jdbc之preparedStatement运用实例
- ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。
- JFreeChart制作图表--饼图
- EasyUI 笔记(2)datagrid 使用
- require for build webrtc
- ZigZag Conversion
- Css Reset(复位)整理
- 解决c语言两个库 函数库冲突
- 中美印日四国程序员比较