web基础小总结

1、新建servlet
名字前面的/servlet删除,这样不容易出错
2、对应属性的名称一定用简单容易记得的,不要出现混乱,不对应,最好方法就是,对应的属性全部起同样的名字,
3、jsp一定和对应的servlet相互对应,在jsp中action=“servlet名字”,servlet最后对应的是要跳转的jsp或者相关操作;
4、实现连接,关闭,增删查修方法总结
public class DBOperator {
public static final String driver = “com.mysql.jdbc.Driver”;
public static final String adress = “jdbc:mysql://localhost:3306/user”;
public static final String user =”root”;
public static final String pass = “root”;//便于修改查看
static{
try {
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection()//连接数据库
{
Connection conn = null;
try {
conn = DriverManager.getConnection(adress, user, pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet r,Statement pst,Connection conn)//关闭
{
try {
if (r != null) {
r.close();
}
if (pst != null) {
pst.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Statement pst,Connection conn)
{
close(null,pst,conn);
}

}
public class ManagerImpls implements Manager{
public boolean add(User u) {//增加数据
boolean falst = false;
try {
Connection conn = DBOperator.getConnection();
String sql = “insert into user(id,password,code,mail,nickname,name,sex,born,adress,position) value(?,?,?,?,?,?,?,?,?,?)”;
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, u.getId());
pst.setString(2,u.getPassword());
pst.setString(3, u.getCode());
pst.setString(4, u.getMail());
pst.setString(5, u.getNickname());
pst.setString(6, u.getName());
pst.setString(7, u.getSex());
pst.setString(8, u.getBorn());
pst.setString(9, u.getAdress());
pst.setString(10, u.getPassword());
int rows = pst.executeUpdate();
if (rows > 0) {
falst = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return falst;
}
public boolean checkLogin(int id,String password) {//登录时进行比较
boolean flag = false;
Statement st = null;
ResultSet r = null;
Connection conn = null;
conn = DBOperator.getConnection();
try {
st = conn.createStatement();
String sql = “select * from user where id= “+id ;
r = st.executeQuery(sql);
while (r.next()) {
if (r.getString(“password”).equals(password)) {
flag = true;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
public List getUser()//获得数据库的所以值
{
List list = new ArrayList();

Connection conn = null;
Statement st = null;
ResultSet r = null;

try {
conn = DBOperator.getConnection();
String sql = “select * from user”;
st = conn.createStatement();
r = st.executeQuery(sql);
while(r.next())
{
User user = new User();
user.setId(r.getInt(“id”));
user.setMail(r.getString(“mail”));
user.setNickname(r.getString(“nickname”));
user.setName(r.getString(“name”));
user.setSex(r.getString(“sex”));
user.setBorn(r.getString(“born”));
user.setAdress(r.getString(“adress”));
user.setPosition(r.getString(“position”));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public boolean delete(int id)//删除数据库的一行数据
{
boolean fault = false;
Connection conn = null;
PreparedStatement pst = null;
conn = DBOperator.getConnection();
String sql = “delete from user where id = ?”;
try {
pst = conn.prepareStatement(sql);
pst.setInt(1, id);
int rows = pst.executeUpdate();
if (rows > 0) {
fault = true;
}
if(fault)
{
fault = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return fault;
}
public User search(int id)//查找数据库id对应的一行信息
{
User user = new User();
Connection conn = null;
Statement st = null;
ResultSet r = null;
try {
conn = DBOperator.getConnection();
String sql = “select * from user where id = ” + id;
st = conn.createStatement();
r = st.executeQuery(sql);
if(r.next())
{
user.setId(r.getInt(“id”));
user.setMail(r.getString(“mail”));
user.setNickname(r.getString(“nickname”));
user.setName(r.getString(“name”));
user.setSex(r.getString(“sex”));
user.setBorn(r.getString(“born”));
user.setAdress(r.getString(“adress”));
user.setPosition(r.getString(“position”));
}
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
public boolean update(User user)//修改id对应的信息
{
boolean fault = false;
Connection conn = null;
PreparedStatement pst = null;
try {
conn = DBOperator.getConnection() ;
String sql = “update user set mail = ?,nickname = ? ,name=?,sex=?,born=?,adress=?,position=? where id = ?”;
pst = conn.prepareStatement(sql);
pst.setString(1, user.getMail());
pst.setString(2, user.getNickname());
pst.setString(3, user.getName());
pst.setString(4, user.getSex());
pst.setString(5, user.getBorn());
pst.setString(6, user.getAdress());
pst.setString(7, user.getPosition());
pst.setInt(8, user.getId());
int rows = pst.executeUpdate();
if(rows>0)
{
fault=true;
}
} catch (Exception e) {
e.printStackTrace();
}
return fault;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋葱ycy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值