JUST GO
[JDBC] TEST 본문
1. 한 번의 쿼리로 아래 데이터를 INSERT 할 수 있도록 작성 후, 실행하세요.
Name | age
김규석 55
김효만 63
김희주 51
김경문 59
김호산 70
package com.rootgo.study_jdbc;
import java.sql.*;
public class Insert {
public static void main(String[] args) throws
ClassNotFoundException,
SQLException {
Connection connection = DatabaseUtils.getConnection();
try (connection) {
try (PreparedStatement preparedStatement = connection.prepareStatement(
// PreparedStatement(인터페이스) : 쿼리문 작성하기에 준비된 상태
"INSERT INTO `study`.`jdbc` (`name`,`age`) " +
"VALUES ('김일일', 55)," +
"('김일이', 63)," +
"('김일삼', 51)," +
"('김일사', 59)," +
"('김일오', 70)")) {
int a = preparedStatement.executeUpdate();
System.out.println(a);
} catch (SQLException ex) {
System.out.println("안에서 터짐 ");
}
} catch(SQLException ex){
System.out.println("밖에서 터짐 ");
}
}
}
2. 전체 SELECT 하여, 김씨만 출력하도록 하세요. 이 때, SELECT 쿼리는 현재 상태에서 변화 되어서는 안 됩니다.
package com.rootgo.study_jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Select {
public static void main(String[] args) throws
ClassNotFoundException,
SQLException {
try (Connection connection = DatabaseUtils.getConnection()){
try (PreparedStatement preparedStatement = connection.prepareStatement("" +
"SELECT `name` AS `name`, " +
" `age` AS `age` " +
"FROM `study`.`jdbc` " +
"ORDER BY `name` ")) {
try (ResultSet resultSet = preparedStatement.executeQuery()){
while (resultSet.next()) {
String name = resultSet.getString("name");
// String familyName = resultSet.getString("김%");
int age = resultSet.getInt("age");
if (name.startsWith("김")) {
System.out.printf("%s, %d\n", name, age);
}
}
}
}
}
}
}
'Java > TEST' 카테고리의 다른 글
[Java] TEST8 (0) | 2022.10.25 |
---|---|
[Java] TEST7 (0) | 2022.10.20 |
[Java] TEST6 (0) | 2022.10.20 |
[Java] TEST5 (0) | 2022.10.19 |
[Java] TEST4 (0) | 2022.10.19 |