create database member;
use member;
create table notice (
id int,
title varchar(100),
writer_id varchar(50),
content longtext,
regdate timestamp,
hit int,
files varchar(1000)
);
create table comment (
id int,
content varchar(2000),
regdate timestamp,
writer_id varchar(50),
notice_id int
);
create table role(
id varchar(50),
discription varchar(500)
);
create table member_role (
member_id varchar(50),
role_id varchar(50)
);
create table member (
id varchar(50),
pwd varchar(50),
name varchar(50),
gender char(2),
birthday char(10),
phone char(13),
regdate date,
email varchar(200)
);
insert into notice values (1, 'JDBC란 무엇인가?', 'yoon', 'aaa', sysdate(), 0, '');
insert into notice values (2, 'JDBC란 무엇인가?2', 'yoon2', 'aaa', sysdate(), 0, '');
insert into notice values (3, 'JDBC란 무엇인가?3', 'yoon3', 'aaa', sysdate(), 0, '');
package ex1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class Program {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/member?";
String sql = "select * from notice";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "password");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String writerId = rs.getString("writer_id");
Date regDate = rs.getDate("regdate");
String content = rs.getString("content");
int hit = rs.getInt("hit");
System.out.printf(" id: %d \n title: %s \n writerId: %s \n regDate: %s \n content: %s \n hit: %d \n",
id, title, writerId, regDate, content, hit);
System.out.println("-------------------------------");
}
rs.close();
st.close();
con.close();
}
}
id: 1
title: JDBC란 무엇인가?
writerId: yoon
regDate: 2021-10-08
content: aaa
hit: 0
-------------------------------
id: 2
title: JDBC란 무엇인가?2
writerId: yoon2
regDate: 2021-10-08
content: aaa
hit: 0
-------------------------------
id: 3
title: JDBC란 무엇인가?3
writerId: yoon3
regDate: 2021-10-08
content: aaa
hit: 0
-------------------------------
SET SQL_SAFE_UPDATES = 0;
update notice set hit = 13 where id = 1;
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String writerId = rs.getString("writer_id");
Date regDate = rs.getDate("regdate");
String content = rs.getString("content");
int hit = rs.getInt("hit");
//수정부분
if (hit > 10) {
System.out.printf(" id: %d \n title: %s \n writerId: %s \n regDate: %s \n content: %s \n hit: %d \n",
id, title, writerId, regDate, content, hit);
System.out.println("-------------------------------");
}
}
package ex1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class Program {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/member?";
//수정부분
String sql = "select * from notice where hit > 10";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "password");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String writerId = rs.getString("writer_id");
Date regDate = rs.getDate("regdate");
String content = rs.getString("content");
int hit = rs.getInt("hit");
System.out.printf(" id: %d \n title: %s \n writerId: %s \n regDate: %s \n content: %s \n hit: %d \n",
id, title, writerId, regDate, content, hit);
System.out.println("-------------------------------");
}
rs.close();
st.close();
con.close();
}
//수정부분
String sql = "select * from notice where hit > 10";
애초부터 쿼리문을 이렇게 하면 된다.