typedao.java
来自「模拟网上购物系统」· Java 代码 · 共 102 行
JAVA
102 行
package dao;
import java.sql.*;
import java.util.ArrayList;
import po.TypePO;
public class TypeDAO {
Connection conn = null;
Statement state = null;
ResultSet rs = null;
//遍历Type表的所有记录
public ArrayList findAll()
{
ArrayList array = new ArrayList();
TypePO po = null;
try {
conn = Tools.getConnection();
state = conn.createStatement();
rs = state.executeQuery("select * from type");
while(rs.next())
{
po = new TypePO();
po.setTypeid(rs.getInt("typeid"));
po.setTypename(rs.getString("typename"));
array.add(po);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(rs != null)
rs.close();
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return array;
}
//增加栏目
public boolean addType(TypePO po)
{
boolean isok = false;
try {
conn = Tools.getConnection();
state = conn.createStatement();
state.executeUpdate("insert into type values("+po.getTypeid()+",'"+po.getTypename()+"')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isok;
}
//自增ID
public int NextID()
{
int id = 0;
try {
conn = Tools.getConnection();
state = conn.createStatement();
rs = state.executeQuery("select count(*) max from type");
if(rs.next())
{
id = rs.getInt("max");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(rs != null)
rs.close();
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ++id;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?