• 数据库连接方式比较


    Oracle连接方式

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class DBConnection {
        public DBConnection(){
            driver="oracle.jdbc.driver.OracleDriver";
            url="jdbc:oracle:thin:@localhost:1521:myoracle";
            dbusername="system";
            dbpassword="admin";
            this.getConection();
        }
        
        private String driver;
        private String url;
        private String dbusername;
        private String dbpassword;
        private Connection con;
        private Statement sta;
        private ResultSet rs;
        
        /*
         * 得到Connection对象。
         */
        public Connection getConection(){
            try {
                Class.forName(driver);
                con=DriverManager.getConnection(url,dbusername,dbpassword);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(SQLException e){
                e.printStackTrace();
            }
            return con;
        }
        
        /*
         * 得到Statement对象
         */
        public Statement getStatement(){
            try {
                sta=con.createStatement();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return sta;
        }
    }

    SQL SERVER

    package com.wy.tools;
    
    import java.sql.*;
    
    /**
     * 
     * @author Administrator
     */
    public class JDBConnection {
        private final String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
        private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_register";
        private final String userName = "sa";
        private final String password = "";
        private ResultSet rs = null;
        private Statement stmt = null;
        private Connection con = null;
    
        public JDBConnection() {
            try {
                Class.forName(dbDriver).newInstance();
            } catch (Exception ex) {
                System.out.println("数据库加载失败");
            }
        }
    
        private boolean creatConnection() {
            try {
                con = DriverManager.getConnection(url, userName, password);
                con.setAutoCommit(true);
                return true;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                return false;
            }
            
        }
    
        public boolean executeUpdate(String sql) {
            if (null == con) {
                creatConnection();
            }
            try {
                stmt = con.createStatement();
                stmt.executeUpdate(sql);
                return true;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                return false;
            }
        }
    
        public ResultSet executeQuery(String sql) {
            try {
                if (null == con) {
                    creatConnection();
                }
                stmt = con.createStatement();
                try {
                    rs = stmt.executeQuery(sql);
                    return rs;
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                    return null;
                }
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                return null;
            }    
        }
    
        public void closeConnection() {
            if (null != rs) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != stmt) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != con) {
                try {
                    con.close();
                } catch (SQLException e) {
                } finally {
                    con = null;
                }
            }
        }
    }
  • 相关阅读:
    Python 字典(Dictionary)
    Python函数
    Python 模块
    一步步学习SPD2010--第二章节--处理SP网站(9)---- 关键点
    一步步学习SPD2010--第二章节--处理SP网站(8)---- 保存和使用网站模板
    一步步学习SPD2010--第二章节--处理SP网站(7)---- 导航网站的内容
    一步步学习SPD2010--第二章节--处理SP网站(6)---- 探索SP网站
    一步步学习SPD2010--第二章节--处理SP网站(5)附—升级自定义SP网站的复杂性
    一步步学习SPD2010--第二章节--处理SP网站(5)--重置网站模板
    一步步学习SPD2010--第二章节--处理SP网站(4)--删除SP网站
  • 原文地址:https://www.cnblogs.com/haimishasha/p/6067541.html
Copyright © 2020-2023  润新知