package gz.itcast.jdbc;
import gz.itcast.jdbc.entity.Student;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import org.junit.Test;
/*
* prepareStatement
*/
public class Demo2 {
@Test
public void test1() {
Connection conn=null;
PreparedStatement pre=null;
try {
//获取连接对象
conn=jdbcUtil.getConnection();
//准备sql语句 添加
//String sql="INSERT INTO student(NAME,gender) VALUES(?,?)"; //占位符
//更新
/* 更新 */
// String sql="UPDATE student SET NAME=? WHERE id=?;";
/* 删除 */
String sql = "DELETE FROM student WHERE id=6;";
//创建prepareStatement
pre=conn.prepareStatement(sql);
//添加参数 从1开始
pre.setString(1, "赵六");
pre.setString(2, "男");
//执行命令
int count=pre.executeUpdate();
System.out.println("影响的行数:"+count+"行");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}finally{
jdbcUtil.close(conn, pre);
}
}
@Test
public void test2() {
Connection conn = null;
PreparedStatement statement = null;
ResultSet reSet = null;
try {
// 获取连接对象
conn = jdbcUtil.getConnection();
// 查询sql语句
String sql = "SELECT * FROM student;";
// 创建statement对象
statement = conn.prepareStatement(sql);
// 读取数据
reSet = statement.executeQuery(sql);
java.util.List<Student> list = new ArrayList<Student>();
while (reSet.next()) {
Student stu = new Student();
stu.setId(reSet.getInt("id"));
stu.setName(reSet.getString("name"));
stu.setGender(reSet.getString("gender"));
list.add(stu);
}
for (Student stu : list) {
System.out.println(stu.getId());
System.out.println(stu.getName());
System.out.println(stu.getGender());
}
} catch (Exception e) {
jdbcUtil.close(conn, statement, reSet);
}
}
}
import gz.itcast.jdbc.entity.Student;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import org.junit.Test;
/*
* prepareStatement
*/
public class Demo2 {
@Test
public void test1() {
Connection conn=null;
PreparedStatement pre=null;
try {
//获取连接对象
conn=jdbcUtil.getConnection();
//准备sql语句 添加
//String sql="INSERT INTO student(NAME,gender) VALUES(?,?)"; //占位符
//更新
/* 更新 */
// String sql="UPDATE student SET NAME=? WHERE id=?;";
/* 删除 */
String sql = "DELETE FROM student WHERE id=6;";
//创建prepareStatement
pre=conn.prepareStatement(sql);
//添加参数 从1开始
pre.setString(1, "赵六");
pre.setString(2, "男");
//执行命令
int count=pre.executeUpdate();
System.out.println("影响的行数:"+count+"行");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}finally{
jdbcUtil.close(conn, pre);
}
}
@Test
public void test2() {
Connection conn = null;
PreparedStatement statement = null;
ResultSet reSet = null;
try {
// 获取连接对象
conn = jdbcUtil.getConnection();
// 查询sql语句
String sql = "SELECT * FROM student;";
// 创建statement对象
statement = conn.prepareStatement(sql);
// 读取数据
reSet = statement.executeQuery(sql);
java.util.List<Student> list = new ArrayList<Student>();
while (reSet.next()) {
Student stu = new Student();
stu.setId(reSet.getInt("id"));
stu.setName(reSet.getString("name"));
stu.setGender(reSet.getString("gender"));
list.add(stu);
}
for (Student stu : list) {
System.out.println(stu.getId());
System.out.println(stu.getName());
System.out.println(stu.getGender());
}
} catch (Exception e) {
jdbcUtil.close(conn, statement, reSet);
}
}
}