-> primary key (userId)
->
-> );
Query OK, 0 rows affected (0.53 sec)
mysql>
mysql> insert into users values('javajigi', 'password', '자바지기', 'javajigi@
ipp.net');
Query OK, 1 row affected (0.07 sec)
mysql> show tables;
+---------------------+
| Tables_in_slipp_dev |
+---------------------+
| users |
+---------------------+
1 row in set (0.02 sec)
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| userId | varchar(12) | NO | PRI | NULL | |
| password | varchar(12) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| email | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.04 sec)
mysql>
class UserDAO
package com.slipp.user;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class UserDAO {
Connection conn=null;
public static Connection getConnection(){
String url="jdbc:mysql://localhost:3305/slipp_dev?useSSL=false";
String id="slipp";
String pw="1111";
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver exist");
conn=DriverManager.getConnection(url, id,pw);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public void insert(User TEST_USER) {
String sql="insert into USERS (userId, password , name , email) values(?, ? , ? , ?)";
PreparedStatement pstmt=null;
try{
conn=UserDAO.getConnection();
pstmt=getConnection().prepareStatement(sql);
pstmt.setString(1, TEST_USER.getUserid());
pstmt.setString(2, TEST_USER.getPassword());
pstmt.setString(3, TEST_USER.getName());
pstmt.setString(4, TEST_USER.getEmail());
pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(pstmt!=null)pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
try{
if(conn!=null)pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class UserDAOTest
package com.slipp.user;
import static org.junit.Assert.assertNull;
import java.sql.Connection;
import org.junit.Test;
public class UserDAOTest {
UserDAO dao=new UserDAO();
@Test
public void connection() {
Connection conn =UserDAO.getConnection();
assertNull(conn);
}
@Test
public void insertTest(){
dao.insert(UserTest.TEST_USER);
}
}
데이터 삽입 성공
mysql> select * from users;
+----------+----------+--------------+--------------------+
| userId | password | name | email |
+----------+----------+--------------+--------------------+
| javajigi | password | 자바지기 | javajigi@slipp.net |
| userid | password | name | java@rok.net |
+----------+----------+--------------+--------------------+
2 rows in set (0.00 sec)
mysql>
댓글 ( 4)
댓글 남기기