zoukankan      html  css  js  c++  java
  • jdbc 操作步骤详解

    package com.itheima.test;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    import org.junit.Test;
    
    public class JdbcTest2 {
    
        @Test
        public void testAdd(){
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                //1.加载驱动 Driver------static代码块就有注册驱动    1.可以避免2次注册驱动   2.不会依赖于mysql驱动jar
                Class.forName("com.mysql.jdbc.Driver");//反射原理创建对象 创建Driver类的对象
                
                //2.创建连接
                //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/day15", "root", "root");
                
                Properties p= new Properties();
                p.put("user", "root");//设置用户名  key可以参考Mysql 文档26.3.3
                p.put("password", "root");//设置密码
                 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/day15", p);
                
                //3.得到用于发送和执行sql语句的对象 Statement
                 st = con.createStatement();
                
                //4.执行语句
                //boolean flag = st.execute("insert into t1 values(5,'test5')");//CRUD   返回值代表是否有结果集  有结果集返回true  没有结果集返回false
                boolean flag = st.execute("select * from t1");
            
                if(flag){
                    System.out.println("有结果集");
                    //5.处理结果
                     rs = st.getResultSet();//得到结果集
                    /*while(rs.next()){
                        System.out.println(rs.getObject(1)+","+rs.getObject(2));
                    }*/
                    
                     //从后往前输出  先定位到最后一行的后面一个位置  afterLast()     不断向前走  previous()
                     rs.afterLast();//最后一行的后面一个位置
                     while(rs.previous()){
                         System.out.println(rs.getObject(1)+","+rs.getObject(2));
                     }
                    
                }else{
                    System.out.println("没有结果集");
                }
                
        
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                //6.关闭资源
                /*try {
                    if(rs!=null){
                        rs.close();
                        rs=null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    try {
                        if(st!=null){
                            st.close();
                            st=null;
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }finally{
                        try {
                            
                            if(con!=null){
                                con.close();
                                con=null;
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }*/
                
                
                try {
                    if(rs!=null){
                        rs.close();
                        rs=null;//目的是让回收器立即进行垃圾回收
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
                try {
                    if(st!=null){
                        st.close();
                        st=null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
                try {
                    if(con!=null){
                        con.close();
                        con=null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
            }
            
            
        }
    }
  • 相关阅读:
    基于摸板匹配的目標跟蹤算法
    spoj 2713 Can you answer these queries IV
    zoj 3633 Alice's present
    hdu 3642 Get The Treasury
    poj 1195 Mobile phones
    poj 2760 End of Windless Days
    zoj 3540 Adding New Machine
    spoj 1716 Can you answer these queries III
    spoj 1043 Can you answer these queries I
    spoj 2916 Can you answer these queries V
  • 原文地址:https://www.cnblogs.com/baijin05/p/5069822.html
Copyright © 2011-2022 走看看