jdbc链接数据库
Posted donghongbo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc链接数据库相关的知识,希望对你有一定的参考价值。
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链接数据库的主要内容,如果未能解决你的问题,请参考以下文章