先导入架包
连接数据库
DBAccess
package com.yq.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 提供了一组获得或关闭数据库对象的方法
*
*/
public class DBAccess {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
InputStream is = DBAccess.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获得数据连接对象
*
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBAccess.getConnection();
DBAccess.close(conn);
System.out.println("isOracle:" + isOracle());
System.out.println("isSQLServer:" + isSQLServer());
System.out.println("isMysql:" + isMysql());
System.out.println("数据库连接(关闭)成功");
}
}
在这里插入代码片
StudentDao
package com.yq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.yq.entity.Student;
import com.yq.entity.Teacher;
import com.yq.util.BaseDao;
import com.yq.util.DBAccess;
import com.yq.util.PageBean;
import com.yq.util.StringUtils;
public class StudentDao extends BaseDao<Student>{
/**
* 用户查询方法(带分页的)
* @param book
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Student> list(Student stu, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from student where true ";
String name=stu.getName();
String tname=stu.getTeacher();
String banji=stu.getBanji();
String hobby=stu.getHobby();
int id = stu.getId();
if(StringUtils.isNotBlank(name)) {
sql += " and name like '%"+name+"%'";
}
// if(StringUtils.isNotBlank(tname)) {
// sql += " and teacher like '%"+tname+"%'";
// }
// if(StringUtils.isNotBlank(banji)) {
// sql += " and banji like '%"+banji+"%'";
// }
// if(StringUtils.isNotBlank(hobby)) {
// sql += " and hobby like '%"+hobby+"%'";
// }
if(id != 0) {
sql += " and id ="+id;
}
return super.executeQuery(sql, Student.class, pageBean);
}
/**
* 拿到教师表里面的所有数据
* @return
*/
public List<Teacher> listtea() {
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Teacher> list=new ArrayList<>();
try {
con=DBAccess.getConnection();
ps=con.prepareStatement("select * from teacher");
rs=ps.executeQuery();
while(rs.next()) {
Teacher tea=new Teacher();
tea.setTid(rs.getInt("tid"));
tea.setName(rs.getString("name"));
list.add(tea);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 拿到爱好表里面的所有数据
* @return
*/
public List<Hobby> listhobby() {
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Hobby> list=new ArrayList<>();
try {
con=DBAccess.getConnection();
ps=con.prepareStatement("select * from hobby");
rs=ps.executeQuery();
while(rs.next()) {
Hobby tea=new Hobby();
tea.setId(rs.getInt("id"));
tea.setName(rs.getString("name"));
list.add(tea);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 拿到所有班级表的数据
* @return
*/
public List<Banji> listbanji() {
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Banji> list=new ArrayList<>();
try {
con=DBAccess.getConnection();
ps=con.prepareStatement("select * from banji");
rs=ps.executeQuery();
while(rs.next()) {
Banji bj=new Banji();
bj.setCid(rs.getInt("cid"));
bj.setName(rs.getString("name"));
list.add(bj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 修改方法
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int edit (Student stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAcc