package db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.util.ArrayList;
public class ConnDb {
private static Connection con = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void startconn() { // 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?characterEncoding=utf-8",
"root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void endconn() throws SQLException { // 关闭连接
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (con != null) {
con.close();
con = null;
}
}
public static void update(String sql) throws SQLException {
startconn();
stmt = con.createStatement();
stmt.executeUpdate(sql);
endconn();
}
public static ArrayList<Student> getList1(String sql) throws SQLException {
ArrayList<Student> list = new ArrayList<Student>();
startconn();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Student st = new Student();
st.setSid(rs.getString("sid"));
st.setName(rs.getString("sname"));
st.setAge(rs.getString("sage"));
list.add(st);
}
endconn();
return list;
}
}
- 1
- 2
- 3
前往页