JDBC数据库的API对照实例学习



  1. 功能:  
  2.       实现数据库对数据的批处理,比如下面要输入一千条数据,不能每次都要创建连接等操作之后插入一条再断开再建立插入、、、、这样的话很显然是十分的浪费时间的。  
  3.       当然了,批处理并不一定能到达很高的效率但是这是一种解决问题的方式。  
  4.   
  5. 时间:20131003  
  6. 作者:烟大阳仔  
  7. */  
  8. public class PiChuLi {  
  9.   
  10.     public static void main(String[] args) throws SQLException {  
  11.         // TODO Auto-generated method stub  
  12.         create();  
  13.     }  
  14.     static void create() throws SQLException  
  15.     {  
  16.         Connection conn=null;  
  17.         PreparedStatement ps=null;  
  18.         ResultSet resultset=null;  
  19.           
  20.         try {  
  21.             //2.建立连接  
  22.             conn=JdbcUtils.getConnection();  
  23.               
  24.             //3.创建语句  
  25.             String sql="insert into user(name,birthday,money) values(?,?,?)";  
  26.             ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
  27.             for(int i=0;i<1000;i++)  
  28.             {  
  29.                 ps.setString(1, "sdmf"+i);  
  30.                 ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));  
  31.                 ps.setFloat(3, 345+i);  
  32.                 ps.addBatch();  
  33.             }  
  34.             ps.executeBatch();  
  35.               
  36.         } finally  
  37.         {  
  38.             JdbcUtils.free(resultset, ps, conn);  
  39.         }  
  40.     }  
  41. }  
  42. ----------------------------------------------------------------------------------------------------------  
  43. /*  
  44. 功能:  
  45.       拿到刚插入的信息的主键,这是API中的一个用于学习该方法  
  46.   
  47. 时间:20131003  
  48. 作者:烟大阳仔  
  49. */  
  50. public class OtherApi {  
  51.   
  52.     public static void main(String[] args) throws SQLException {  
  53.         int id=create();  
  54.         System.out.println(id);  
  55.     }  
  56.     //拿到刚插入的信息的主键  
  57.     static int create() throws SQLException  
  58.     {  
  59.         Connection conn=null;  
  60.         PreparedStatement ps=null;  
  61.         ResultSet resultset=null;  
  62.           
  63.         try {  
  64.             //2.建立连接  
  65.             conn=JdbcUtils.getConnection();  
  66.               
  67.             //3.创建语句  
  68.             String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";  
  69.             ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
  70.             ps.executeUpdate();  
  71.             resultset=ps.getGeneratedKeys();  
  72.             int id=0;  
  73.             if(resultset.next())  
  74.             {  
  75.                 id=resultset.getInt(1);  
  76.             }  
  77.             return id;  
  78.         } finally  
  79.         {  
  80.             JdbcUtils.free(resultset, ps, conn);  
  81.         }  
  82.     }  
  83.   
  84. }  
  85. -------------------------------------------------------------------------------------------------------------  
  86. /*  
  87. 功能:  
  88.       可滚动的结果集实例  
  89.       分页查询  
  90. 时间:20131003  
  91. 作者:烟大阳仔  
  92. */  
  93.   
  94. public class ScrollAPIDemo {  
  95.   
  96.     public static void main(String[] args) throws SQLException {  
  97.         scroll();  
  98.     }  
  99.     //可滚动的结果集实例  
  100.     //分页查询  
  101.     static void scroll() throws SQLException  
  102.     {  
  103.         Connection conn=null;  
  104.         Statement st=null;  
  105.         ResultSet resultset=null;  
  106.           
  107.         try {  
  108.             //2.建立连接  
  109.             conn=JdbcUtils.getConnection();  
  110.             //3.创建语句  
  111.             st=conn.createStatement();  
  112.             //4.执行语句  
  113.             String sql="select id,name,birthday,money from user";  
  114.             resultset=st.executeQuery(sql);  
  115.             resultset.absolute(8);  
  116.             System.out.println();  
  117.             if(resultset.previous())  
  118.             {  
  119.                 System.out.println(  
  120.                 resultset.getObject("id")+"\t"+  
  121.                 resultset.getObject("name")+"\t"+  
  122.                 resultset.getObject("birthday")+"\t"+  
  123.                 resultset.getObject("money")+"\t"  
  124.                   
  125.                 );  
  126.             }  
  127.             //分页的一种方式不过效率比较低MySQL本身支持分页  
  128.             //对MySQL来说设置分页的话直接在sql语句中写为:  
  129.             //select id,name,birthday,money from user limit 100,10  
  130.             //也就是定位到第一百条数据显示十条数据  
  131.             resultset.absolute(100);  
  132.             int i=0;  
  133.             while(resultset.next()&&i<10)  
  134.             {  
  135.                 i++;  
  136.                 System.out.println(  
  137.                         resultset.getObject("id")+"\t"+  
  138.                         resultset.getObject("name")+"\t"+  
  139.                         resultset.getObject("birthday")+"\t"+  
  140.                         resultset.getObject("money")+"\t"  
  141.                         );  
  142.             }  
  143.         } finally  
  144.         {  
  145.             JdbcUtils.free(resultset, st, conn);  
  146.         }  
  147.     }  
  148. }  
  149. -------------------------------------------------------------------------------------------------------------  
  150. /*  
  151. 功能:  
  152.       可更新的结果集  
  153. 时间:20131003  
  154. 作者:烟大阳仔  
  155. */  
  156.   
  157. public class UpdateKeGengXin {  
  158.   
  159.     /**  
  160.      * @param args  
  161.      * @throws SQLException   
  162.      */  
  163.     public static void main(String[] args) throws SQLException {  
  164.         // TODO Auto-generated method stub  
  165.         read();  
  166.     }  
  167.     //可更新的结果集,查询出来之后进行更新,不过这个了解就可以了  
  168.     static void read() throws SQLException  
  169.     {  
  170.         Connection conn=null;  
  171.         Statement st=null;  
  172.         ResultSet resultset=null;  
  173.           
  174.         try {  
  175.             //2.建立连接  
  176.             conn=JdbcUtils.getConnection();  
  177.               
  178.             //3.创建语句  
  179.             st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
  180.             //4.执行语句  
  181.             resultset=st.executeQuery("select id,name,birthday,money from user where id<10");  
  182.             //5.处理结果  
  183.             while(resultset.next())  
  184.             {  
  185.                 System.out.println(resultset.getObject("id"));  
  186.                 System.out.println(resultset.getObject("name"));  
  187.                 System.out.println(resultset.getObject("birthday"));  
  188.                 System.out.println(resultset.getObject("money"));  
  189.                 String name=resultset.getString("name");  
  190.                 if("wangwu".equals(name))  
  191.                 {  
  192.                     resultset.updateFloat("money", 123);  
  193.                     resultset.updateRow();  
  194.                 }  
  195.             }  
  196.               
  197.         } finally  
  198.         {  
  199.             JdbcUtils.free(resultset, st, conn);  
  200.         }  
  201.     }  
  202. }  
  203. -----------------------------------------------------------------------------------------------------------  
  204. /*  
  205. 功能:  
  206.       数据库的元数据信息  
  207. 时间:20131003  
  208. 作者:烟大阳仔  
  209. */  
  210.   
  211. public class DBMD {  
  212.     //数据库的原信息  
  213.     public static void main(String[] args) throws SQLException {  
  214.         Connection conn=JdbcUtils.getConnection();  
  215.         DatabaseMetaData dbmd=conn.getMetaData();  
  216.         System.out.println(dbmd.getDatabaseMajorVersion());  
  217.         //数据库的名称  
  218.         System.out.println(dbmd.getDatabaseProductName());  
  219.         //数据库的版本号  
  220.         System.out.println(dbmd.getDatabaseProductVersion());  
  221.         //是不是支持事务型  
  222.         System.out.println(dbmd.supportsTransactions());  
  223.           
  224.     }  
  225.   
  226. }  
  1. 功能:  
  2.       实现数据库对数据的批处理,比如下面要输入一千条数据,不能每次都要创建连接等操作之后插入一条再断开再建立插入、、、、这样的话很显然是十分的浪费时间的。  
  3.       当然了,批处理并不一定能到达很高的效率但是这是一种解决问题的方式。  
  4.   
  5. 时间:20131003  
  6. 作者:烟大阳仔  
  7. */  
  8. public class PiChuLi {  
  9.   
  10.     public static void main(String[] args) throws SQLException {  
  11.         // TODO Auto-generated method stub  
  12.         create();  
  13.     }  
  14.     static void create() throws SQLException  
  15.     {  
  16.         Connection conn=null;  
  17.         PreparedStatement ps=null;  
  18.         ResultSet resultset=null;  
  19.           
  20.         try {  
  21.             //2.建立连接  
  22.             conn=JdbcUtils.getConnection();  
  23.               
  24.             //3.创建语句  
  25.             String sql="insert into user(name,birthday,money) values(?,?,?)";  
  26.             ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
  27.             for(int i=0;i<1000;i++)  
  28.             {  
  29.                 ps.setString(1, "sdmf"+i);  
  30.                 ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));  
  31.                 ps.setFloat(3, 345+i);  
  32.                 ps.addBatch();  
  33.             }  
  34.             ps.executeBatch();  
  35.               
  36.         } finally  
  37.         {  
  38.             JdbcUtils.free(resultset, ps, conn);  
  39.         }  
  40.     }  
  41. }  
  42. ----------------------------------------------------------------------------------------------------------  
  43. /*  
  44. 功能:  
  45.       拿到刚插入的信息的主键,这是API中的一个用于学习该方法  
  46.   
  47. 时间:20131003  
  48. 作者:烟大阳仔  
  49. */  
  50. public class OtherApi {  
  51.   
  52.     public static void main(String[] args) throws SQLException {  
  53.         int id=create();  
  54.         System.out.println(id);  
  55.     }  
  56.     //拿到刚插入的信息的主键  
  57.     static int create() throws SQLException  
  58.     {  
  59.         Connection conn=null;  
  60.         PreparedStatement ps=null;  
  61.         ResultSet resultset=null;  
  62.           
  63.         try {  
  64.             //2.建立连接  
  65.             conn=JdbcUtils.getConnection();  
  66.               
  67.             //3.创建语句  
  68.             String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";  
  69.             ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
  70.             ps.executeUpdate();  
  71.             resultset=ps.getGeneratedKeys();  
  72.             int id=0;  
  73.             if(resultset.next())  
  74.             {  
  75.                 id=resultset.getInt(1);  
  76.             }  
  77.             return id;  
  78.         } finally  
  79.         {  
  80.             JdbcUtils.free(resultset, ps, conn);  
  81.         }  
  82.     }  
  83.   
  84. }  
  85. -------------------------------------------------------------------------------------------------------------  
  86. /*  
  87. 功能:  
  88.       可滚动的结果集实例  
  89.       分页查询  
  90. 时间:20131003  
  91. 作者:烟大阳仔  
  92. */  
  93.   
  94. public class ScrollAPIDemo {  
  95.   
  96.     public static void main(String[] args) throws SQLException {  
  97.         scroll();  
  98.     }  
  99.     //可滚动的结果集实例  
  100.     //分页查询  
  101.     static void scroll() throws SQLException  
  102.     {  
  103.         Connection conn=null;  
  104.         Statement st=null;  
  105.         ResultSet resultset=null;  
  106.           
  107.         try {  
  108.             //2.建立连接  
  109.             conn=JdbcUtils.getConnection();  
  110.             //3.创建语句  
  111.             st=conn.createStatement();  
  112.             //4.执行语句  
  113.             String sql="select id,name,birthday,money from user";  
  114.             resultset=st.executeQuery(sql);  
  115.             resultset.absolute(8);  
  116.             System.out.println();  
  117.             if(resultset.previous())  
  118.             {  
  119.                 System.out.println(  
  120.                 resultset.getObject("id")+"\t"+  
  121.                 resultset.getObject("name")+"\t"+  
  122.                 resultset.getObject("birthday")+"\t"+  
  123.                 resultset.getObject("money")+"\t"  
  124.                   
  125.                 );  
  126.             }  
  127.             //分页的一种方式不过效率比较低MySQL本身支持分页  
  128.             //对MySQL来说设置分页的话直接在sql语句中写为:  
  129.             //select id,name,birthday,money from user limit 100,10  
  130.             //也就是定位到第一百条数据显示十条数据  
  131.             resultset.absolute(100);  
  132.             int i=0;  
  133.             while(resultset.next()&&i<10)  
  134.             {  
  135.                 i++;  
  136.                 System.out.println(  
  137.                         resultset.getObject("id")+"\t"+  
  138.                         resultset.getObject("name")+"\t"+  
  139.                         resultset.getObject("birthday")+"\t"+  
  140.                         resultset.getObject("money")+"\t"  
  141.                         );  
  142.             }  
  143.         } finally  
  144.         {  
  145.             JdbcUtils.free(resultset, st, conn);  
  146.         }  
  147.     }  
  148. }  
  149. -------------------------------------------------------------------------------------------------------------  
  150. /*  
  151. 功能:  
  152.       可更新的结果集  
  153. 时间:20131003  
  154. 作者:烟大阳仔  
  155. */  
  156.   
  157. public class UpdateKeGengXin {  
  158.   
  159.     /**  
  160.      * @param args  
  161.      * @throws SQLException   
  162.      */  
  163.     public static void main(String[] args) throws SQLException {  
  164.         // TODO Auto-generated method stub  
  165.         read();  
  166.     }  
  167.     //可更新的结果集,查询出来之后进行更新,不过这个了解就可以了  
  168.     static void read() throws SQLException  
  169.     {  
  170.         Connection conn=null;  
  171.         Statement st=null;  
  172.         ResultSet resultset=null;  
  173.           
  174.         try {  
  175.             //2.建立连接  
  176.             conn=JdbcUtils.getConnection();  
  177.               
  178.             //3.创建语句  
  179.             st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
  180.             //4.执行语句  
  181.             resultset=st.executeQuery("select id,name,birthday,money from user where id<10");  
  182.             //5.处理结果  
  183.             while(resultset.next())  
  184.             {  
  185.                 System.out.println(resultset.getObject("id"));  
  186.                 System.out.println(resultset.getObject("name"));  
  187.                 System.out.println(resultset.getObject("birthday"));  
  188.                 System.out.println(resultset.getObject("money"));  
  189.                 String name=resultset.getString("name");  
  190.                 if("wangwu".equals(name))  
  191.                 {  
  192.                     resultset.updateFloat("money", 123);  
  193.                     resultset.updateRow();  
  194.                 }  
  195.             }  
  196.               
  197.         } finally  
  198.         {  
  199.             JdbcUtils.free(resultset, st, conn);  
  200.         }  
  201.     }  
  202. }  
  203. -----------------------------------------------------------------------------------------------------------  
  204. /*  
  205. 功能:  
  206.       数据库的元数据信息  
  207. 时间:20131003  
  208. 作者:烟大阳仔  
  209. */  
  210.   
  211. public class DBMD {  
  212.     //数据库的原信息  
  213.     public static void main(String[] args) throws SQLException {  
  214.         Connection conn=JdbcUtils.getConnection();  
  215.         DatabaseMetaData dbmd=conn.getMetaData();  
  216.         System.out.println(dbmd.getDatabaseMajorVersion());  
  217.         //数据库的名称  
  218.         System.out.println(dbmd.getDatabaseProductName());  
  219.         //数据库的版本号  
  220.         System.out.println(dbmd.getDatabaseProductVersion());  
  221.         //是不是支持事务型  
  222.         System.out.println(dbmd.supportsTransactions());  
  223.           
  224.     }  
  225.   
  226. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值