haohan类
package com.westos.haohan;
public class haohan {
private int xuhao;
private String name;
public void setName(String name) {
this.name = name;
}
public void setXuhao(int xuhao) {
this.xuhao = xuhao;
}
public Integer getXuhao() {
return xuhao;
}
public String getName() {
return name;
}
@Override
public String toString() {
return super.toString();
}
}
haohaninterface接口
package com.westos.haohan;
import java.util.List;
public interface haohaninterface {
haohan findhaohan(int xuhao);
List<haohan> listhaohan();
haohan add(haohan hh);
haohan update(haohan hh);
haohan delete(int xuhao);
}
haohanImpl实现类
package com.westos.haohan;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class haohanImpl implements haohaninterface {
@Override
//显示所有好汉
public List<haohan> listhaohan() {
//连接数据库
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hero?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="123456";
List<haohan> list=new ArrayList<>();
haohan hh=new haohan();
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement ps = con.prepareStatement("select * from heroinfo");
ResultSet rs = ps.executeQuery();
while(rs.next()){
hh.setXuhao(rs.getInt("xuhao"));
hh.setName(rs.getString("name"));
list.add(hh);
}
//关闭资源
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
//根据序号查询好汉
public haohan findhaohan(int xh) {
//连接数据库
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hero?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="123456";
haohan hh=new haohan();
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement ps = con.prepareStatement("select *from heroinfo where xuhao=?");
ps.setInt(1,xh);
ResultSet rs = ps.executeQuery();
while(rs.next()){
hh.setXuhao(rs.getInt("xuhao"));
hh.setName(rs.getString("name"));
}
//关闭资源
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return hh;
}
@Override
//添加好汉
public haohan add(haohan hh) {
//数据库连接
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hero?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="123456";
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement ps = con.prepareStatement("insert into heroinfo(xuhao,name)values(?,?)");
ps.setInt(1,hh.getXuhao());
ps.setString(2,hh.getName());
int i =ps.executeUpdate();
System.out.println(i);
//关闭资源
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return hh;
}
@Override
//修改好汉
public haohan update(haohan hh) {
//数据库连接
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hero?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="123456";
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement ps = con.prepareStatement("update heroinfo set name=? where xuhao=?");
ps.setString(1,hh.getName());
ps.setInt(2,hh.getXuhao());
int i=ps.executeUpdate();
System.out.println(i);
//关闭资源
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return hh;
}
@Override
//删除好汉
public haohan delete(int xh) {
//连接数据库
String driverName="com.musql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hero?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="123456";
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, username, password);
PreparedStatement ps = con.prepareStatement("delete from heroinfo where xuhao=?");
ps.setInt(1,xh);
int i= ps.executeUpdate();
System.out.println(i);
//关闭资源
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
//为了更好的用户体验,返回一个信息
haohan hh=new haohan();
hh.setXuhao(xh);//这个序号里面的内容已经空了
return hh;
}
//测试
public static void main(String[]args){
haohaninterface haohanservice=new haohanImpl();
haohan hh=new haohan();
// //测试添加
// hh.setXuhao(4);
// hh.setName("宋江kdd");
// haohan result= haohanservice.add(hh);
//测试修改
// hh.setXuhao(1);
// hh.setName("卢俊义");
// haohan result=haohanservice.update(hh);
//测试删除
// haohan result=haohanservice.delete(2);
//测试查询
//haohan result=haohanservice.findhaohan(3);
//测试显示所有好汉
List result=haohanservice.listhaohan();
System.out.println(result);
}
}