StuentDao
public class StudentDao {
public List<StudentVO> select(){
String sql2 = "select * from student";
Execute execute = new Execute();
ResultSet rs = execute.executeQuery(sql2);
List<StudentVO> stuList = new ArrayList<>();
try {
while (rs.next()) {
System.out.println(rs.getInt("id"));
StudentVO stu = new StudentVO();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stuList.add(stu);
}
} catch(SQLException se){
throw new RuntimeException(se);
} finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return stuList;
}
public int insert(StudentVO stu){
String sql = "insert into student values(" + stu.getId() + ",'" + stu.getName() + "',"
+ stu.getAge() + ",'" + stu.getAddress() + "','" + stu.getIdentify() + "','" + stu.getCountry() + "')";
Execute execute = new Execute();
int i = execute.executeUpdate(sql);
return i;
}
}
Execute :
import java.sql.*;
public class Execute {
public int executeUpdate(String sql){
PreparedStatement preparedStatement=null;
try {
preparedStatement =GetConnertion.con.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
int num = 0;
try {
num = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return num;
}
public ResultSet executeQuery(String sql) {
PreparedStatement preparedStatement=null;
try {
preparedStatement =GetConnertion.con.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
ResultSet rs = null;
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return rs;
}
}
GetConnertion类
public static Connection con = null;
static{//找驱动
try {
Class.forName(JDBCLeading.DRIVER);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
DriverManager.getConnection(JDBCLeading.URL,JDBCLeading.USER,JDBCLeading.PASSWORD);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
JDBCLeading JBDC的properties
import com.jdbcPro.JDBCTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class JDBCLeading {
public static String DRIVER=null;
public static String USER = null;
public static String PASSWORD = null;
public static String URL = null;
public JDBCLeading(){
InputStream in = JDBCTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
DRIVER = properties.getProperty("Driver");
URL=properties.getProperty("url");
USER=properties.getProperty("user");
PASSWORD =properties.getProperty("password");
}
}
JBDC studentVO
public class StudentVO {
@Override
public String toString() {
return "StudentVO{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", identify='" + identify + '\'' +
", country='" + country + '\'' +
'}';
}
private int id;
private String name;
private int age;
private String address;
private String identify;
private String country;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getIdentify() {
return identify;
}
public void setIdentify(String identify) {
this.identify = identify;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public StudentVO() {
}
public StudentVO(int id, String name, int age, String address, String identify, String country) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.identify = identify;
this.country = country;
}
测试类"
import com.jdbcPro.dao.StudentDao;
import com.jdbcPro.vo.StudentVO;
import java.util.List;
public class test01 {
public static void main(String[] args) {
StudentDao studentDao = new StudentDao();
List<StudentVO> list = studentDao.select();
System.out.println(list.size());
StudentVO stu = new StudentVO(338, "zhang", 16, "shandong", "asdas", "zhongguo");
int num = studentDao.insert(stu);
System.out.println(num);
}
}