- JDBC
- java가 데이터베이스와 통신 할 수 있게 해주는 api
- JDBC 실행순서
- Driver loading -> Connection -> Statement -> query -> run
- PrepareStatement
- PreparedStatement pstmt = null;
- String sql = "update book set book_loc = ? where book_name = ?
- pstmt = con.prepareStatement(sql);
- pstmt.setString(1, "***"); // 매개변수로 쿼리 안에 들어가야 할 값을 전달 한다
- Select 로직
- public class JDBC_select
{
public static void main(String[] args)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String Server = "서버주소";
String UserName = "서버아이디";
String Password = "서버비밀번호";
String Database = "사용할 데이터베이스";
String Driver = "org.mariadb.jdbc.Driver";
try{
/*
* Driver error 확인
*/
Class.forName(Driver);
/*
*Connection 클래스 통한 db 연결
*/
con = DriverManager.getConnection("jdbc:mysql://" +
Server + "/" +
Database +
"?useSSL=false", UserName, Password);
/*
* Bookdb Select Query 전송
*/
String sql = "select * from book";
/*
* prepareStatement 사용한 쿼리 조작
*/
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
/*
* Database내 자료가 있을때까지 반복문 진행
*/
while (rs.next())
{
int book_id = rs.getInt("book_id");
String book_name = rs.getString("book_name");
String book_loc = rs.getString("book_loc");
System.out.println("책 index : " + book_id + " 책 이름 : " + book_name + " 책 위치 : " + book_loc);
}
}catch (SQLException e)
{
System.out.print("연결 실패 :" + e.getMessage());
}
catch (ClassNotFoundException e)
{
System.out.print("드라이버 에러 : " + e.getMessage());
}
catch(Exception e)
{
System.out.print("기타 에러 : " + e.getMessage());
}
try
{
if(con != null)
{
con.close();
}
if(pstmt != null)
{
pstmt.close();
}
if(rs != null)
{
rs.close();
}
System.out.print("Database disconnect");
}
catch (SQLException e)
{
System.out.print("접속 종료 에러 : " + e.getMessage());
}
}
} - Insert Query
- public class JDBC_Insert
{
public static void main(String[] args) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String Server = "";
String UserName = "";
String Password = "";
String Database = "";
String Driver = "org.mariadb.jdbc.Driver";
try {
/*
* Driver error 확인
*/
Class.forName(Driver);
/*
*Connection 클래스 통한 db 연결
*/
con = DriverManager.getConnection("jdbc:mysql://" +
Server + "/" +
Database +
"?useSSL=false", UserName, Password);
Scanner stdIn = new Scanner(System.in);
/*
* 임시 form submit 대체 Scanner 클래스 사용
*/
System.out.println("책 제목 입력 : " );
String book_name = stdIn.nextLine();
System.out.println("책 위치 입력 : ");
String book_loc = stdIn.nextLine();
String sql = "insert into book (book_name, book_loc) values (?, ?)";
pstmt = con.prepareStatement(sql);
/*
* prepareStatement 객체 사용한 insert query 밸류 값 지정
*/
pstmt.setString(1, book_name);
pstmt.setString(2, book_loc);
/*
* insert query Database 전송
*/
int result = pstmt.executeUpdate();
if(result == 1)
{
System.out.println("insert-sucess");
}
else
{
System.out.println("insert-fail");
}
} catch (SQLException e) {
System.out.print("연결 에러 : " + e.getMessage());
} catch (ClassNotFoundException e){
System.out.println("드라이버 에러 : " + e.getMessage());
} catch (Exception e){
System.out.println("기타 에러 : " + e.getMessage());
}
try {
if(con != null)
{
con.close();
}
if(pstmt != null)
{
pstmt.close();
}
System.out.println("Databases Disconnect");
}catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
}
- public class JDBC_Insert
- public class JDBC_select
'JAVA' 카테고리의 다른 글
Servlet 특징 정리 (0) | 2021.07.21 |
---|---|
DAO, DTO (0) | 2021.07.17 |
JSP - 한글 처리 (0) | 2021.07.05 |
Session (0) | 2021.07.01 |
Cookie (0) | 2021.06.30 |