zoukankan      html  css  js  c++  java
  • jdbc操作元数据

    package cn.code.jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    public class NumberFour {
        @Test
        /**
         * 操作元数据
         * */
         public void fun(){
             String url="jdbc:mysql://localhost:3306/mydb1";
             String driverclassname="com.mysql.jdbc.Driver";
             String user="root";
             String password="123";
             Connection con=null;
             Statement s=null;
             ResultSet r=null;
             try{
                 con=DriverManager.getConnection(url, user, password);
                 s= con.createStatement();//s =con.createStatement(int,int);这个方法用来控制结果集的三大属性是否可滚动、是否敏感、是否可更新
                 String sql="select * from stu";
                 r=s.executeQuery(sql);
                 //获取元数据
                 ResultSetMetaData rmd = r.getMetaData();
                 //获取列数
                 int count = rmd.getColumnCount();
                 //遍历行
                 while(r.next()){
                     for(int i =1;i<=count;i++){//遍历列
                        Object o = r.getObject(i);//获取每行第i列数据
                        System.out.print(o);
                         if(i<count){
                             System.out.print(",");
                         }
                     }
                     System.out.println();
                 }
             }catch(SQLException e){
                 throw new RuntimeException(e);
             }finally{
                 if(r!=null)
                    try {
                        r.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                 if(s!=null)
                    try {
                        s.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                 if(con!=null)
                    try {
                        con.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             }
                 
         }
    }
    package cn.code.data;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    public class selectData {
        @Test
        public void fun() throws SQLException {
            Connection con =null;
            Statement s =null;
            ResultSet r =null;
            try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","123");
            s = con.createStatement();
            String sql="SELECT * FROM stu";
            r = s.executeQuery(sql);
            r.last();//把光标移动到最后一个行
            System.out.println(r.getRow());//显示当前光标所在行、间接获取到结果集行数
            r.beforeFirst();//光标返回首行之前一行,默认行;
            int cell = r.getMetaData().getColumnCount();//r.getMetaData()获取元数据ResultSetMetaData,获取列数;
                while(r.next()){
                    for(int i=1;i<=cell;i++){      //遍历结果集
                        System.out.print(r.getString(i));
                        if(i<cell){System.out.print(",");}
                    }
                    System.out.println();
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                //关闭资源,倒关
                if(r!=null)r.close();
                if(s!=null)s.close();
                if(con!=null)con.close();//这个必须关
            }
            
        }
        
    }
  • 相关阅读:
    PHP多进程(四) 内部多进程
    STL map and multimap
    Understanding Function Objects
    Working with Bit Flags Using STL
    STL Algorithms
    STL set and multiset
    Understanding Smart Pointers
    More Effective C++ 学习笔记(1)
    Adaptive Container: stack and queue
    第一个 Python 程序
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/7402111.html
Copyright © 2011-2022 走看看