📄 teldao.java
字号:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TelDAO {
Statement stmt;
public TelDAO() throws SQLException{
this.stmt=CreateConnection.conn.createStatement();
}
public boolean insert(String name,String tel) throws SQLException{
String sql="insert into tel values('"+name+"','"+tel+"')";
int info=this.stmt.executeUpdate(sql);
if(info>0){
return true;
}else{
return false;
}
}
public boolean update(int index,String name,String tel) throws SQLException{
String sql="update tel set name='"+name+"',tel='"+tel+"' where id="+index;
int info=this.stmt.executeUpdate(sql);
return info>0;
}
public boolean delete(int id) throws SQLException{
String sql="delete from tel where id='"+id+"'";
int info=this.stmt.executeUpdate(sql);
return info>0;
}
public List findByName(String name) throws SQLException{
String sql="select * from tel where name='"+name+"'";
ResultSet rs=this.stmt.executeQuery(sql);
List telList=new ArrayList();
while(rs.next()){
TelBean telBean=new TelBean();
telBean.setName(rs.getString("name"));
telBean.setTel(rs.getString("tel"));
telBean.setId(rs.getInt("id"));
telList.add(telBean);
}
return telList;
}
public List findAll() throws SQLException{
String sql="select * from tel order by name";
ResultSet rs=this.stmt.executeQuery(sql);
List telList=new ArrayList();
while(rs.next()){
TelBean telBean=new TelBean();
telBean.setName(rs.getString("name"));
telBean.setTel(rs.getString("tel"));
telBean.setId(rs.getInt("id"));
telList.add(telBean);
}
return telList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -