Prepared Statement 을 사용 하여 삭제 와 검 사 를 실현 합 니 다.
package com.power.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import com.power.util.DBUtil;
/**
* @author chengwei
* @date 2017 7 13 4:09:44
* @description:
*/
public class StudentDao {
/**
*
*/
public void insert(Student student) {
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "INSERT INTO student(student_id, name, gender, age) VALUES(?,?,?,?)";
try {
connection = DBUtil.getConnection();
// sql
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, student.getId());
preparedStatement.setString(2, student.getName());
preparedStatement.setString(3, student.getGender());
preparedStatement.setInt(4, student.getAge());
// sql
int resultNum = preparedStatement.executeUpdate();
if (resultNum > 0) {
System.out.println(" !");
}
} catch (SQLException e) {
System.out.println(" ");
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.out.println(" ");
}
}
}
/**
*
*/
public void delete(Integer studentId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "DELETE FROM student WHERE student_id = ?";
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, studentId);
int resultNum = preparedStatement.executeUpdate();
if (resultNum > 0) {
System.out.println(" !");
}
} catch (SQLException e) {
System.out.println(" ");
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.out.println(" ");
}
}
}
/**
*
*/
public void list(int start, int size) {
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "SELECT s.student_id, s.name, s.gender, s.age FROM student s LIMIT ?,?";
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, start);
preparedStatement.setInt(2, size);
ResultSet result = preparedStatement.executeQuery();
while (result.next()) {
Student student = new Student(result.getInt("student_id"), result.getString("name"),
result.getString("gender"), result.getInt("age"));
System.out.println(student);
}
} catch (SQLException e) {
System.out.println(" ");
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.out.println(" ");
}
}
}
/**
*
*/
public static void main(String[] args) {
StudentDao dao = new StudentDao();
Student student = new Student(106, " ", "W", 26);
dao.insert(student);
dao.list(0, 5);
dao.delete(106);
}
}
/**
*
*/
class Student {
/** */
private Integer id;
/** */
private String name;
/** */
private String gender;
/** */
private Integer age;
public Student(Integer id, String name, String gender, Integer age) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.