JDBC数据库的API对照实例学习
来源:互联网 发布:php 活动报名 源代码 编辑:程序博客网 时间:2024/06/06 12:33
- /*
- 功能:
- 实现数据库对数据的批处理,比如下面要输入一千条数据,不能每次都要创建连接等操作之后插入一条再断开再建立插入、、、、这样的话很显然是十分的浪费时间的。
- 当然了,批处理并不一定能到达很高的效率但是这是一种解决问题的方式。
- 时间:20131003
- 作者:烟大阳仔
- */
- public class PiChuLi {
- public static void main(String[] args) throws SQLException {
- // TODO Auto-generated method stub
- create();
- }
- static void create() throws SQLException
- {
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet resultset=null;
- try {
- //2.建立连接
- conn=JdbcUtils.getConnection();
- //3.创建语句
- String sql="insert into user(name,birthday,money) values(?,?,?)";
- ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
- for(int i=0;i<1000;i++)
- {
- ps.setString(1, "sdmf"+i);
- ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));
- ps.setFloat(3, 345+i);
- ps.addBatch();
- }
- ps.executeBatch();
- } finally
- {
- JdbcUtils.free(resultset, ps, conn);
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------
- /*
- 功能:
- 拿到刚插入的信息的主键,这是API中的一个用于学习该方法
- 时间:20131003
- 作者:烟大阳仔
- */
- public class OtherApi {
- public static void main(String[] args) throws SQLException {
- int id=create();
- System.out.println(id);
- }
- //拿到刚插入的信息的主键
- static int create() throws SQLException
- {
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet resultset=null;
- try {
- //2.建立连接
- conn=JdbcUtils.getConnection();
- //3.创建语句
- String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";
- ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
- ps.executeUpdate();
- resultset=ps.getGeneratedKeys();
- int id=0;
- if(resultset.next())
- {
- id=resultset.getInt(1);
- }
- return id;
- } finally
- {
- JdbcUtils.free(resultset, ps, conn);
- }
- }
- }
- -------------------------------------------------------------------------------------------------------------
- /*
- 功能:
- 可滚动的结果集实例
- 分页查询
- 时间:20131003
- 作者:烟大阳仔
- */
- public class ScrollAPIDemo {
- public static void main(String[] args) throws SQLException {
- scroll();
- }
- //可滚动的结果集实例
- //分页查询
- static void scroll() throws SQLException
- {
- Connection conn=null;
- Statement st=null;
- ResultSet resultset=null;
- try {
- //2.建立连接
- conn=JdbcUtils.getConnection();
- //3.创建语句
- st=conn.createStatement();
- //4.执行语句
- String sql="select id,name,birthday,money from user";
- resultset=st.executeQuery(sql);
- resultset.absolute(8);
- System.out.println();
- if(resultset.previous())
- {
- System.out.println(
- resultset.getObject("id")+"\t"+
- resultset.getObject("name")+"\t"+
- resultset.getObject("birthday")+"\t"+
- resultset.getObject("money")+"\t"
- );
- }
- //分页的一种方式不过效率比较低MySQL本身支持分页
- //对MySQL来说设置分页的话直接在sql语句中写为:
- //select id,name,birthday,money from user limit 100,10
- //也就是定位到第一百条数据显示十条数据
- resultset.absolute(100);
- int i=0;
- while(resultset.next()&&i<10)
- {
- i++;
- System.out.println(
- resultset.getObject("id")+"\t"+
- resultset.getObject("name")+"\t"+
- resultset.getObject("birthday")+"\t"+
- resultset.getObject("money")+"\t"
- );
- }
- } finally
- {
- JdbcUtils.free(resultset, st, conn);
- }
- }
- }
- -------------------------------------------------------------------------------------------------------------
- /*
- 功能:
- 可更新的结果集
- 时间:20131003
- 作者:烟大阳仔
- */
- public class UpdateKeGengXin {
- /**
- * @param args
- * @throws SQLException
- */
- public static void main(String[] args) throws SQLException {
- // TODO Auto-generated method stub
- read();
- }
- //可更新的结果集,查询出来之后进行更新,不过这个了解就可以了
- static void read() throws SQLException
- {
- Connection conn=null;
- Statement st=null;
- ResultSet resultset=null;
- try {
- //2.建立连接
- conn=JdbcUtils.getConnection();
- //3.创建语句
- st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- //4.执行语句
- resultset=st.executeQuery("select id,name,birthday,money from user where id<10");
- //5.处理结果
- while(resultset.next())
- {
- System.out.println(resultset.getObject("id"));
- System.out.println(resultset.getObject("name"));
- System.out.println(resultset.getObject("birthday"));
- System.out.println(resultset.getObject("money"));
- String name=resultset.getString("name");
- if("wangwu".equals(name))
- {
- resultset.updateFloat("money", 123);
- resultset.updateRow();
- }
- }
- } finally
- {
- JdbcUtils.free(resultset, st, conn);
- }
- }
- }
- -----------------------------------------------------------------------------------------------------------
- /*
- 功能:
- 数据库的元数据信息
- 时间:20131003
- 作者:烟大阳仔
- */
- public class DBMD {
- //数据库的原信息
- public static void main(String[] args) throws SQLException {
- Connection conn=JdbcUtils.getConnection();
- DatabaseMetaData dbmd=conn.getMetaData();
- System.out.println(dbmd.getDatabaseMajorVersion());
- //数据库的名称
- System.out.println(dbmd.getDatabaseProductName());
- //数据库的版本号
- System.out.println(dbmd.getDatabaseProductVersion());
- //是不是支持事务型
- System.out.println(dbmd.supportsTransactions());
- }
- }
0 0
- JDBC数据库的API对照实例学习
- JDBC数据库的API对照实例学习
- JDBC数据库的API对照实例学习
- JDBC API的使用方法及各种数据库jdbc字符串写法
- jdbc连接数据库和jdbc的核心api
- jdbc连接各种数据库的代码实例
- 关于API SDK和MFC的对照学习
- 关于API SDK和MFC的对照学习
- Jdbc 连接数据库 实例
- JDBC连接数据库实例
- Spring+JDBC 数据库实例
- android api 完整翻译之Contacts Provider (学习安卓必知的api,中英文对照)
- android api 完整通译之Contacts Provider (学习安卓必知的api,中英文对照)
- android api 完整翻译之Contacts Provider (学习安卓必知的api,中英文对照)
- 通过JDBC API 访问mysql数据库的servlet范例程序
- Java JDBC链接数据库实例
- jdbc简单操作数据库实例
- JDBC连接Oracle数据库实例
- 第四天、婚礼上的谎言
- 进程通信的方式
- Android Duplicate files copied in APK
- LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- java类库中的File类使用
- JDBC数据库的API对照实例学习
- springmvc处理表单数据到后台
- Hyper-V配置(二)
- UITableView创建
- WapApp初始化样式表
- C++继承方式
- JDBC实现文件、图片的存储实例
- 从Eclipse到Android Studio:Android项目如何进行迁移
- 【BZOJ2759】一个动态树好题