[2] JDBC, 첨삭 및 수정 CRUD 구현
3126 단어 jdbc
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CRUD {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
// create();
read();
// update();
// delete();
}
static void delete() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.
st = conn.createStatement();
String sql = "delete from user where id>4";
// 4.
int i = st.executeUpdate(sql);
System.out.println("i=" + i);
} finally {
JdbcUtils.free(rs, st, conn);
}
}
static void update() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.
st = conn.createStatement();
String sql = "update user set money=money+10 ";
// 4.
int i = st.executeUpdate(sql);
System.out.println("i=" + i);
} finally {
JdbcUtils.free(rs, st, conn);
}
}
static void create() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.
st = conn.createStatement();
String sql = "insert into user(name,birthday, money) values ('name1', '1987-01-01', 400) ";
// 4.
int i = st.executeUpdate(sql);
System.out.println("i=" + i);
} finally {
JdbcUtils.free(rs, st, conn);
}
}
static void read() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.
st = conn.createStatement();
// 4.
rs = st.executeQuery("select id, name, money, birthday from user");
// 5.
while (rs.next()) {
System.out.println(rs.getObject("id") + "\t"
+ rs.getObject("name") + "\t"
+ rs.getObject("birthday") + "\t"
+ rs.getObject("money"));
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 애플리케이션을 모든 SQL 데이터베이스와 연결그래서 오늘 이 기사를 통해 JDBC를 통해 Java 애플리케이션을 SQL 데이터베이스에 연결하기 위해 작성할 수 있는 각 줄을 설명하는 심층 가이드를 제공하여 그들과 모든 커뮤니티를 도우려고 합니다. JDBC는 J...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.