📄 huitiedao.java
字号:
package com.zjy.struts.mypackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.zjy.struts.form.HuitieForm;
import com.zjy.struts.mybean.Fenye;
public class HuitieDao {
public Connection con = null;
public PreparedStatement ps = null;
public Statement stmt = null;
public ResultSet rs = null;
public HuitieDao() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=luntan";
String user = "sa";
String password = "";
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO: handle exception
}
}
public List setHuitie(Fenye fy){
List list=new ArrayList();
try {
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql="select * from huitie where parentid="+fy.getId();
rs=stmt.executeQuery(sql);
int a=1;
if(fy.getShowpage()==1){
}else{
rs.absolute((fy.getShowpage()-1)*fy.getShowlist());
a=(fy.getShowpage()-1)*fy.getShowlist()+1;
}
for(int i=0;i<5;i++){
if(rs.next()){
HuitieForm hf=new HuitieForm();
hf.setId(a+i);
hf.setParentid(rs.getString(1));
hf.setName(rs.getString(3));
hf.setText(huanhang(rs.getString(4)));
hf.setTimes(rs.getString(5));
list.add(hf);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return list;
}
public int insertHuitie(HuitieForm huitieForm) {
Date t = new Date();
String ts = t.toLocaleString();
int rs=0;
try {
ps = con
.prepareStatement("insert into huitie(parentid,name, text, times) values(?,?,?,?)");
ps.setInt(1, Integer.parseInt(huitieForm.getParentid()));
ps.setString(2, bx(huitieForm.getName()));
ps.setString(3, bx(huitieForm.getText()));
ps.setString(4, ts);
rs=ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}finally{
try {
ps.close();
stmt.close();
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return rs;
}
public int deleteHuitei(String id){
int rst=0;
try {
int ids=Integer.parseInt(id);
stmt=con.createStatement();
String sql="delete from huitie where id="+ids;
rst=stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stmt.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
return rst;
}
public int deleteAll(String id){
int rst=0;
try {
int ids=Integer.parseInt(id);
stmt=con.createStatement();
String sql="delete from huitie where parentid="+ids;
rst=stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stmt.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
return rst;
}
public String huanhang(String s){//换行
s = s.replace("\r\n", "<br>");
return s;
}
public String bx(String s) {
try {
byte b[] = s.getBytes("iso-8859-1");
s = new String(b);
} catch (Exception e) {
}
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -