Demo entry 6730958

DbUtil

   

Submitted by anonymous on Apr 10, 2018 at 03:20
Language: Java. Code size: 1.9 kB.

package jdbc;

import java.sql.*;

//----------------------------------------------------------------------------------------------------    
public class DbUtil {
	public DbUtil() {

	}

	public Connection getConnection(int index) {
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url, username, password;
			switch (index) {
			case 1:
				url = "jdbc:oracle:thin:@10.2.1.91:1521:ecology";
				username = "ecologynew";
				password = "ecology";
				conn = DriverManager.getConnection(url, username, password);
				System.out.println("连接到:" + url + "成功了!");
				break;
			case 2:
				Class.forName("oracle.jdbc.driver.OracleDriver");
				url = "jdbc:oracle:thin:@10.2.1.107:1521:ecology";
				username = "jtit";
				password = "jf6247";
				conn = DriverManager.getConnection(url, username, password);
				System.out.println("连接到:" + url + "成功了!");
				break;

			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	// ----------------------------------------------------------------------------------------------------
	public static void close(PreparedStatement pstmt) {
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
	}
	// ----------------------------------------------------------------------------------------------------
	public static void close(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
	}
	// ----------------------------------------------------------------------------------------------------
	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).