千家信息网

jdbc.utils QueryRunner的建档使用示例

发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,这篇文章主要介绍了jdbc.utils QueryRunner的建档使用示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。packag
千家信息网最后更新 2025年01月18日jdbc.utils QueryRunner的建档使用示例

这篇文章主要介绍了jdbc.utils QueryRunner的建档使用示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

package com.jdbc.test;import java.sql.*;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.ResultSetHandler;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;public class OdbcTest {@SuppressWarnings({ "unchecked", "rawtypes" })public static void main(String[] args) {Connection conn =null;try {Class.forName("oracle.jdbc.driver.OracleDriver");conn  =  DriverManager.getConnection("jdbc:oracle:thin:@192.168.249.137:1521:hgs","hgs","hgs");    //DriverManager.getConnection("jdbc:oracle:thin:@192.168.249.137:1521:hgs","hgs","hgs");//Class.forName("oracle.jdbc.driver.OracleDriver");//Connection connection = null;//connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.249.137:1521:hgs","hgs","hgs");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}QueryRunner qr = new QueryRunner();////////////////////查询try {List ai = qr.query(conn, "select atype,averageage from animals",new BeanListHandler(Animals.class));   for(Animals a : ai){   System.out.println("atype: "+a.getAtype() +" avgage: "+a.getAverageage());   }} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}////////////////////////////////////插入try {Object[] params= {"man",78};//qr.insert(conn, "insert into animals(atype,averageage) values(?,?)", new BeanHandler(Animals.class), params);qr.update(conn, "insert into animals(atype,averageage) values(?,?)", params);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}///删除try {Object[] params= {"man",78};//qr.insert(conn, "insert into animals(atype,averageage) values(?,?)", new BeanHandler(Animals.class), params);qr.update(conn, "delete from animals");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}      }}

感谢你能够认真阅读完这篇文章,希望小编分享的"jdbc.utils QueryRunner的建档使用示例"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

0