码迷,mamicode.com
首页 > 数据库 > 详细

jdbc链接数据库

时间:2017-08-24 15:01:22      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:java   log   out   int   word   public   dex   color   工具   

package com.maya.hanqi.jdbctest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConnectUtil { //工具类
    private static final String USERNAME = "test";
    private static final String PASSWORD = "test";
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
    private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver";
    
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(DRIVERCLASSNAME);
            conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }        
        return conn;        
    }
    public static void destroy(Connection conn,Statement sm,ResultSet rs){
        if (conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
        if (sm!=null) {
            try {
                sm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
        if (rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
    public static void main(String[] args) {
        System.out.println(getConnection());
        
    }
}

 

jdbc链接数据库

标签:java   log   out   int   word   public   dex   color   工具   

原文地址:http://www.cnblogs.com/donghb/p/7422832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!