HD

JAVA DB Connection (feat. mysql, oracle) 본문

JAVA

JAVA DB Connection (feat. mysql, oracle)

hunecenter 2020. 12. 28. 13:15
반응형

MYSQL Connection

mysql-connector-java-5.1.48.jar
0.96MB

public static Connection  getMySqlCon(String dbUser, String dbPwd, String schema) throws SQLException {
		
		String url = "jdbc:mysql://localhost:3306/?characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true";
		if(schema!=null) {
			url = "jdbc:mysql://localhost:3306/"+schema+"?characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true";
		}
		String user = dbUser!=null?dbUser:"test";
		String pwd = dbPwd!=null?dbPwd:"test1234";
		Connection  conn = null;
		try {		
			Class.forName("org.gjt.mm.mysql.Driver");
			conn = DriverManager.getConnection(url,user,pwd);
			System.out.println("DB getExCon success!!");
		} catch (Exception e){
			e.printStackTrace();
			System.out.println("DB getExCon Error!!");
		}
		return conn;
	}

 

ORACLE Connection

ojdbc6.jar
2.61MB

public static Connection getOracleCon() throws SQLException {
		
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		String user = "test";
		String pwd = "test1234";
		
		Connection  conn = null;
		try {		
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url,user,pwd);
			System.out.println("DB getNewCon success!!");
		} catch (Exception e){
			e.printStackTrace();
			System.out.println("DB getNewCon Error!!");
		}
		return conn;
	}
반응형
Comments