Dao.java
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Student;
public interface Dao {
//增加
int insert(Student stu);
//删除
int delete(Student stu);
//修改
int update(Student stu);
//查询
List<Student> seleteAll();
//模糊查询
List<Student> seleteName(Student stu);
}
>>> message = '''interpreter
... prompt'''
DaoBase.java
package com.bdqn.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.bdqn.utils.ConfigManager;
public class DaoBase {
//获取连接
public static Connection getConnection(){
Connection conn = null;
String driver = ConfigManager.getInstance().getValue("driver");
String url = ConfigManager.getInstance().getValue("url");
String user = ConfigManager.getInstance().getValue("user");
String pwd = ConfigManager.getInstance().getValue("pwd");
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭资源
public void close(Connection conn, Statement sta, ResultSet rs){
try {
if(conn!=null){
conn.close();
}
if(sta!=null){
sta.close();
}
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新数据
public int execute(String sql, Object[] obj){
int num=0;
Connection conn = getConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
ps.setObject(i+1, obj[i]);
}
num = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, ps, null);
}
return num;
}
//查询数据
public ResultSet selete(String sql){
ResultSet rs = null;
Connection conn = getConnection();
try {
rs = conn.createStatement().executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
>>> message = '''interpreter
... prompt'''
Impl.java
package com.bdqn.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.bdqn.dao.Dao;
import com.bdqn.dao.DaoBase;
import com.bdqn.entity.Student;
public class Impl extends DaoBase implements Dao{
//添加数据
public int insert(Student stu) {
String sql = "insert into student values(?,?,?,?,?)";
Object[] obj = {stu.getSid(),stu.getSname(),stu.getSage(),stu.getShobby(),stu.getGid()};
int num = super.execute(sql, obj);
return num;
}
//删除
public int delete(Student stu) {
String sql = "delete from student where sid = ?";
Object[] obj = {stu.getSid()};
int num = super.execute(sql, obj);
return num;
}
//更新
public int update(Student stu) {
String sql = "update student set sname=? where sid=?";
Object[] obj = {stu.getSname(),stu.getSid()};
int num = super.execute(sql, obj);
return num;
}
//查询
public List<Student> seleteAll() {
ArrayList<Student> list = new ArrayList<Student>();
String sql = "select *from student";
ResultSet rs = super.selete(sql);
try {
while(rs.next()){
Student stu = new Student();
stu.setSid(rs.getInt("sid"));
stu.setSname(rs.getString("sname"));
stu.setSage(rs.getInt("sage"));
stu.setShobby(rs.getString("shobby"));
stu.setGid(rs.getInt("gid"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public List<Student> seleteName() {
ArrayList<Student> list = new ArrayList<Student>();
String sql = "selete *from student where sname like '%2%'";
ResultSet rs = super.selete(sql);
try {
while(rs.next()){
Student stu = new Student();
stu.setSid(rs.getInt("sid"));
stu.setSname(rs.getString("sname"));
stu.setSage(rs.getInt("sage"));
stu.setShobby(rs.getString("shobby"));
stu.setGid(rs.getInt("gid"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public List<Student> seleteName(Student stu) {
return null;
}
}
>>> message = '''interpreter
... prompt'''
Student.java
package com.bdqn.entity;
public class Student {
private int sid;
private String sname;
private int sage;
private String shobby;
private int gid;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getShobby() {
return shobby;
}
public void setShobby(String shobby) {
this.shobby = shobby;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public Student(int sid, String sname, int sage, String shobby, int gid) {
this.sid = sid;
this.sname = sname;
this.sage = sage;
this.shobby = shobby;
this.gid = gid;
}
public Student() {
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage
+ ", shobby=" + shobby + ", gid=" + gid + "]";
}
}
>>> message = '''interpreter
... prompt'''
Test.java
package com.bdqn.entity;
import java.util.List;
import com.bdqn.dao.impl.Impl;
public class Test {
public static void main(String[] args) {
Impl impl =new Impl();
int num =0;
impl.insert(new Student(1, "01", 20, "01", 1));
impl.insert(new Student(2, "02", 20, "02", 1));
impl.insert(new Student(3, "03", 20, "03", 2));
/*List<Student> list = impl.seleteAll();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
Student stu1 = new Student();
stu1.setSid(1);
num = impl.delete(stu1);
System.out.println(num);
Student stu2 = new Student();
stu2.setSname("修改名字");
stu2.setSid(2);
System.out.println(impl.update(stu2));
List<Student> list2 = impl.seleteName();
for (int i = 0; i < list2.size(); i++) {
System.out.println(list.get(i).toString());
}*/
}
}
>>> message = '''interpreter
... prompt'''
Server.java
package com.bdqn.server;
import java.io.Serializable;
import java.util.List;
import com.bdqn.entity.Student;
public interface Server extends Serializable{
public List<Student> findAll();
}
>>> message = '''interpreter
... prompt'''
ServerImpl.java
package com.bdqn.server.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.bdqn.dao.DaoBase;
import com.bdqn.entity.Student;
import com.bdqn.server.Server;
public class ServerImpl implements Server{
public List<Student> findAll() {
DaoBase db = new DaoBase();
List<Student> list = new ArrayList<Student>();
String sql = "select *from student";
ResultSet rs = db.selete(sql);
try {
while(rs.next()){
Student stu = new Student();
stu.setGid(rs.getInt("gid"));
stu.setSage(rs.getInt("sage"));
stu.setShobby(rs.getString("shobby"));
stu.setSid(rs.getInt("sid"));
stu.setSname(rs.getString("sname"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
>>> message = '''interpreter
... prompt'''
ConfigManager.java
package com.bdqn.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ConfigManager {
private static Properties prop;
private static ConfigManager config;
InputStream is;
//私有构造
private ConfigManager(){
prop = new Properties();
try {
is = ConfigManager.class.getClassLoader().getResourceAsStream("database.properties");
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//获得ConfigManager对象
public static synchronized ConfigManager getInstance(){
if(config==null){
synchronized (ConfigManager.class) {
if(config==null){
config = new ConfigManager();
}
}
}
return config;
}
//获取配置文件的值
public String getValue(String key){
String value = prop.getProperty(key);
return value;
}
}
>>> message = '''interpreter
... prompt'''
database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student
user=root
pwd=root
>>> message = '''interpreter
... prompt'''
findAll.jsp
<%@ page import="com.bdqn.entity.*" %>
<%@ page import="com.bdqn.server.*" %>
<%@ page import="com.bdqn.server.impl.*" %>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<% Server si = new ServerImpl(); %>
<% List<Student> list = si.findAll(); %>
<table border=1>
<tr>
<td>年级编号</td>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>兴趣</td>
</tr>
<% for(Student stu:list){ %>
<tr>
<td><%=stu %></td>
<td><%=stu.getSid() %></td>
<td><%=stu.getSname() %></td>
<td><%=stu.getSage() %></td>
<td><%=stu.getShobby() %></td>
</tr>
<%} %>
</table>
</body>
</html>
>>> message = '''interpreter
... prompt'''