-
Notifications
You must be signed in to change notification settings - Fork 0
/
JDBC_Connect
32 lines (29 loc) · 1.03 KB
/
JDBC_Connect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.sql.*;
public class DBInsert {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection conn = null;
Statement stmt;
ResultSet rs = null;
try {
String url = "jdbc:mysql://localhost:3306/store?c haracterEncoding=UTF-8&serverTimezone=Asia/Seoul";
String user = "root";
String password = "root";
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(url, user, password);
System.out.println("데이터베이스 연결이 성공했습니다.");
stmt = conn.createStatement();
String sql = "INSERT INTO customer(id, cName, Age, job, deposit) "
+ "VALUES ('E','김학수','30', '목수',10000)";
int i = stmt.executeUpdate(sql);
if (i==1)
System.out.println("레코드 추가 성공");
else
System.out.println("레코드 추가 실패");
}catch(SQLException ex) {
System.out.println("데이터베이스 연결이 실패했습니다.");
} finally {
if(conn != null)
conn.close();
}
}
}