RDBMS : mysql,mssql..등 DBMS : nosql 등 => database 연결 시 라이브러리가 있어야 연결 가능 ( maven repository : 공식사이트 ) connector/J : 구버전(버전 특성 좀 탐, 보안 ↓ , ftp라고 생각하면됨 - 디폴트) - 더많이씀 (mysql-connector-j-8.4.0) connector Java : 버전 특성을 잘 안탐(보안 ↑ , sftp라고 생각하면됨 )
pom : spring 과 사용 jar : servlet과 사용 ** port번호 mysql,mariadb : 3306 mssql : 1433 oracle : 9001~9009 (따로 지정하지 않으면 Port 번호는1521 - 거의 이렇게 사용 x)
** 404 : .do 경로오류 or .jsp 로드오류 500 : Module 오류 (java 문법오류) 403 : controller 오류 (servlet) 402 : loop 발생하여 서버가 정지
⚡ module ( database 접속 환경설정 파일 ) 모듈은 여러개 가능(필요한 모듈만 불러서 작동) database 드라이브 연결 속성 - 외울필요x 다 나와잇음 String db_driver = "com.mysql.cj.jdbc.Driver"; - 라이브러리가 java일경우 String db_driver = "com.mysql.jdbc.Driver"; - 라이브러리가 j일 경우 ..등
jdbc : java database connect 의 약자
- dbconfig.java ( module 파트 )
public Connection info() throws Exception {
//database 드라이브 연결 속성
String db_driver = "com.mysql.jdbc.Driver"; // connector/J
//내부 : localhost , 외부 : ip또는 도메인명을 이용하여 접속
String db_url = "jdbc:mysql://172.30.1.28:3306/cms";
String db_user = "hana"; //사용자 id
String db_pass = "hana1234"; //사용자 pw
Class.forName(db_driver); //어떤 라이브러리를 이용하여 db에 접속할것인가
//-u 아이디 -p
Connection dbcon = DriverManager.getConnection(db_url,db_user,db_pass);
return dbcon; //dbcon을 return
}
testcms.java ( controller 파트- servlert 파일 )
public class testcms extends HttpServlet {
private static final long serialVersionUID = 1L;
//controller part
dbconfig db = new dbconfig(); //해당 module class load
PrintWriter pw = null; //jsp에 alert을 띄우거나.. 등
Connection con = null; //mysql 접속 승인 및 DML,DDL,DCL 사용하기 위함
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
this.con = db.info();
System.out.println("database 접속 성공!");
//table 자동생성 제작 - 해당 변수에 담긴 table명으로 찍어냄
String tables = "notice_se";
//preparedstatement 이라면 "create table ?" 가능
String sql = "create table " + tables +"("
+ "nidx int(5) not null auto_increment,"
+ "mid varchar(30) not null,"
+ "primary key(nidx)"
+ ")";
Statement st = this.con.createStatement(); //선언만 함
boolean result = st.execute(sql);
//insert시에는 해당 문법 사용
//String sql = "insert into "+tables+" values('0','hong12345')";
//PreparedStatement st = this.con.prepareStatement(sql); //int로 받음
//int result = st.executeUpdate();
//if(result > 0) {
//System.out.println("정상적으로 insert 실행되었습니다");
//}
}catch(SQLSyntaxErrorException se) {
System.out.println("중복table 발생 및 SQL문법오류 발생!");
}catch(Exception e) {
System.out.println(e);
System.out.println("database 접속 오류!");
}finally {
try {
this.con.close(); //database 접속 해제(필수)
}catch(Exception e) {
System.out.println("databae를 정상적으로 종료하지 못하였습니다");
}
}
}
}
👀 해당 예제 부가설명
⚡ statement : Connection으로 연결한 객체에게, Query작업을 실행하기 위한 객체. //이게 있어야 sql 작동시킬수 잇음
statement (부모) : SQL문을 실행할 때마다 매번 구문을 새로 해석해서 적용 preparedstatement : 캐시 메모리를 이용하여 보다 빠르게 구문을 작동시킴 / 바인딩 변수를 사용가능 - 여러번 만들때 (select,insert,delete,update.. 등) createstatement : 캐시메모리는 이용은 하나 바인딩변수 불가능, 메모리를 많이 잡아먹음 - 딱 한번만 만들때 (create ,drop,alter...등)
*바인딩변수란 => ?변수 System.out.println("환영합니다" + 변수 + "님"); System.out.println("환영합니다 ?님",변수); - 얘가 바인딩 변수
⚡ excute - Query작업 실행 - excute() : create,drop,alter시(false뜸) 사용 => boolean으로 받음 create,drop,alter,insert,update,delete 시(true뜸), select 시 (true) => 해결방법 : preparedstatement 이걸로 받기