20220308 º¹½À
ÀÚ¹Ù = TUI (Text User Interface) > GUI (Graphic User Interface) > Web > Spring
µ¥ÀÌÅͺ£À̽º ¿¬°á!
ÀÔ·Â
¼öÁ¤
»èÁ¦
Àüü»èÁ¦ truncate
executeUpdate(); = ddl insert, delete, update, truncate
executeQuery(); = select
DB(4´Ü°è)
1. µå¶óÀ̹ö ·Îµå > ¹êµå(ȨÆäÀÌÁö¿¡¼ ´Ù¿î¹Þ°í)
2. ¿¬°á, Äõ¸® (Á¶ÀÛ), ´Ý±â
Connection conn = DriverManager.getConnection();
Statement stmt = conn.createStatement();
stmt.executeQuery();
stmt.executeUpdate();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class T1 {
public static void main(String[] args) {
/*
* JDBC µå¶óÀ̹ö ·Îµå
* µ¥ÀÌÅͺ£À̽º¿Í ¿¬°á
* SQL¹® ½ÇÇà (CRUD > select, insert, update, delete)
* µ¥ÀÌÅͺ£À̽º ¿¬°á ²÷À½
*/
//url : jdbc:subprotocol:subname
String url="jdbc:mysql://localhost:3306/db53";
String id="root";
String pw="123456";
Connection conn=null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("µå¶óÀ̹ö ÀûÀç ¿Ï·á");
conn = DriverManager.getConnection(url,id,pw);
System.out.println("µ¥ÀÌÅͺ£À̽º ¿¬°á ¿Ï·á");
stmt = conn.createStatement();
String sql="INSERT INTO `lego` (`modelNum`, `modelName`, `year`, `maker`, `price`,`age`, `manual`,`reg_date`)
VALUES ('70310', '³ªÀÌÆ° ¿Õ±¹ÀÇ ¹èƲ ºí·¡½ºÅÍ', '2016', '·¹ÄÚÄÚ¸®¾Æ', 30000, 10, 'Y','2012-06-12 22:23:01')";
int i = stmt.executeUpdate(sql);
if(i==1) {
System.out.println("µ¥ÀÌÅÍ ÀÔ·Â ¼º°ø");
}else {
System.out.println("µ¥ÀÌÅÍ ÀÔ·Â ½ÇÆÐ");
}
}catch(ClassNotFoundException e){
System.out.println(e);
}catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}