sortdao.java
来自「网上书店后台管理源码基于struts1.2+oracle数据库」· Java 代码 · 共 235 行
JAVA
235 行
package com.dongfang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.dongfang.po.Sort;
public class SortDAO {
//查询所有的类型
public List getAllSorts()
{
List sortList = new ArrayList();
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
String sql = "select * from sorts";
conn = DBTools.getConn();
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while(rs.next())
{
Sort sort = new Sort();
sort.setId(rs.getInt("id"));
sort.setName(rs.getString("name"));
sortList.add(sort);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(stm!=null)
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sortList;
}
//根据sortid来查询
public Sort getSortById(int sortId)
{
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
Sort sort = new Sort();
String sql = "select * from sorts where id="+sortId+"";
conn = DBTools.getConn();
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
if(rs.next())
{
sort.setId(rs.getInt("id"));
sort.setName(rs.getString("name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(stm!=null)
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sort;
}
public void saveSort(Sort sort)
{
Connection conn = null;
PreparedStatement stm = null;
String sql = "insert into sorts values(?,?)";
conn = DBTools.getConn();
try {
stm = conn.prepareStatement(sql);
stm.setInt(1, sort.getId());
stm.setString(2, sort.getName());
stm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(stm!=null)
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getNextId()
{
int myId = 0;
Connection conn =null;
Statement stm = null;
ResultSet rs = null;
String sql = "select max(id) id from sorts";
conn = DBTools.getConn();
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
if(rs.next())
{
myId = rs.getInt("id");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(stm!=null)
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ++myId;
}
public void delSortById(int sortId)
{
Connection conn =null;
PreparedStatement pstmt = null;
conn = DBTools.getConn();
String sql = "delete sorts where id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, sortId);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?