자바 는 JDBC 로 데이터 베 이 스 를 연결 하 는 다섯 가지 방식(IDEA 버 전)을 사용 합 니 다.
준비 작업
1.mysql 의 jar 패 키 지 를 lib 디 렉 터 리 로 가 져 오기
2.가 져 온 jar 패 키 지 를 항목 에 추가
jar 가방 클릭 선택 하 다.
3.TestConnection 클래스 만 들 기
다섯 가지 방식 은 다음 과 같다.
/**
* @author
* @date 2019
**/
import org.junit.Test;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC
*/
public class ConnectionTest {
//
@Test
public void testConnection1() throws SQLException {
// driver
Driver driver=new com.mysql.jdbc.Driver();
//url;http://localhost:8080/gmall/hello.jpg
String url="jdbc:mysql://localhost:3306/student";
// Properties
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","root");
// info.setProperty("user","root");
// info.setProperty("password","root");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
// ; api
@Test
public void testConnection2() throws Exception{
// driver
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
//2.
String url="jdbc:mysql://localhost:3306/student";
//3.
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","root");
//4.
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
// drivermanager driver
@Test
public void testConneciont3() throws Exception{
//1. Driver
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
//2.
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="root";
//
DriverManager.registerDriver(driver);
//
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
//
@Test
public void testConneciont4() throws Exception{
//1
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="root";
//2. Driver
Class.forName("com.mysql.jdbc.Driver");
// , , Driver
// Driver driver=(Driver) clazz.newInstance();
//
// DriverManager.registerDriver(driver);
//3.
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
// (final) ,
/**
*
* 1. ,
* 2, ,
* @throws Exception
*/
@Test
public void TestConnection5() throws Exception{
//
InputStream is=ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros=new Properties();
pros.load(is);
String user=pros.getProperty("user");
String password=pros.getProperty("password");
String url=pros.getProperty("url");
String driverClass=pros.getProperty("driverClass");
//2.
Class.forName(driverClass);
//3.
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
}
다섯 번 째 방법src 디 렉 터 리 아래 jdbc.properties 파일 을 만 드 는 내용 은 다음 과 같 습 니 다.
그리고 모든 방식 의 실행 결 과 는 연결 성공 을 설명 하 는 것 입 니 다!!!
자바 가 JDBC 를 사용 하여 데이터 베 이 스 를 연결 하 는 다섯 가지 방식(IDEA 버 전)에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 JDBC 연결 데이터 베 이 스 는 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
데이터 시각화 도구 FineReport와 AWS RedShift 연결(JDBC 방법)Amazon Redshift는 클라우드의 완전 관리형, 페타바이트 규모 데이터 웨어하우스 서비스입니다. 수백 기가바이트의 데이터로 시작하여 페타바이트 이상까지 확장할 수 있습니다. 이렇게 하면 고객의 비즈니스와 고객...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.