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

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