zoukankan      html  css  js  c++  java
  • JDBC 你应该知道的事

    JDBC你应该知道的事?

    /*
     * 文 件 名:  DbUtils.java
     * 版    权:  Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
     * 描    述:  <描述>
     * 修 改 人:   * 修改时间:  Apr 19, 2013
     * 跟踪单号:  <跟踪单号>
     * 修改单号:  <修改单号>
     * 修改内容:  <修改内容>
     */
    package com.huawei.jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * 
     * @author   * @version  [版本号, Apr 19, 2013]
     * @see  [相关类/方法]
     * @since  [产品/模块版本]
     */
    public class DbUtils
    {
        private static DbUtils dbUtils = new DbUtils();
        
        private DbUtils()
        {
            
        }
        
        public static DbUtils getInstance()
        {
            return dbUtils;
        }
        
        public void queryExcute()
        {
            
            Connection conn = null;
            Statement stm = null;
            ResultSet result = null;
            try
            {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            try
            {
                conn = DriverManager.getConnection("jdbc:oracle:thin:@10.137.107.160:1521:iptvmem",
                        "userdb30",
                        "I_Vcbs33");
                
                stm = conn.createStatement();
                
                String sql = "select * from ocs_tasktodo2 o where o.subscriberno ='"
                        + "826670842" + "'";
                
                result = stm.executeQuery(sql);
                
                if (null != result)
                {
                    while (result.next())
                    {
                        System.out.println(result.getString("subscriberno"));
                    }
                }
                
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    result.close();
                    stm.close();
                    conn.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            
        }
    }
    


    哈哈,我知道的就这么多,懂一点补充一点

            try
            {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }

    这一行没有任何的设值,孤独的立在代码中,有点奇怪,于是我删掉了这一行。再跑一边,哈哈报错了

    java.sql.SQLException: No suitable driver
    	at java.sql.DriverManager.getConnection(DriverManager.java:545)
    	at java.sql.DriverManager.getConnection(DriverManager.java:171)
    	at com.huawei.jdbc.DbUtils.queryExcute(DbUtils.java:59)
    	at com.huawei.jdbc.DbUtils.main(DbUtils.java:40)
    Exception in thread "main" java.lang.NullPointerException
    	at com.huawei.jdbc.DbUtils.queryExcute(DbUtils.java:87)
    	at com.huawei.jdbc.DbUtils.main(DbUtils.java:40)

    猜测这个是给jvm申明一个Oracle驱动的实例。

    再接着

    conn = DriverManager.getConnection("jdbc:oracle:thin:@10.137.107.160:1521:iptvmem",
                        "userdb30",
                        "I_Vcbs33");

    使用DriverManager建立了一个连接,可以看下其中的方法

        public static synchronized Connection getConnection(String url, 
    	String user, String password) throws SQLException {
            java.util.Properties info = new java.util.Properties();
    
            // Gets the classloader of the code that called this method, may 
    	// be null.
    	ClassLoader callerCL = DriverManager.getCallerClassLoader();
    
    	if (user != null) {
    	    info.put("user", user);
    	}
    	if (password != null) {
    	    info.put("password", password);
    	}
    
            return (getConnection(url, info, callerCL));
        }



    再接下来就是:

                stm = conn.createStatement();
                
                String sql = "select * from ocs_tasktodo2 o where o.subscriberno ='"
                        + "826670842" + "'";
                
                result = stm.executeQuery(sql);
                
                if (null != result)
                {
                    while (result.next())
                    {
                        System.out.println(result.getString("subscriberno"));
                    }
                }
    

    请大神解释一下,从上至下的流程!

  • 相关阅读:
    相遇相知都是缘
    [BTS]6912,5641,5773,5410错误处理!
    [BTS]Cumulative Functloids用法
    职业生涯又一个转折点
    BizTalk Group Day 北京!
    [BTS]10008错误如何处理?
    [ERROR]创建BAM视图时报错
    [BTS]使用BizTalk开发应用系统,就是这么简单!
    [BTS]BizTalk2006 SqlAdapter UpdateGram的Update用法
    [BTS]为什么BTSharePointAdapterWS.asmx无法预览?
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3031508.html
Copyright © 2011-2022 走看看