📄 zhutiedao.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.ZhutieForm;
public class ZhutieDao {
public Connection con = null;
public PreparedStatement ps = null;
public Statement stmt = null;
public ResultSet rs = null;
public ZhutieDao() {
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 ZhutieForm setZhutie(int id){
//List list=new ArrayList();
ZhutieForm zf=new ZhutieForm();
try {
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql="select * from zhutie where id="+id;
rs=stmt.executeQuery(sql);
rs.next();
zf.setId(id);
zf.setName(rs.getString(2));
zf.setTitle(rs.getString(3));
zf.setText(huanhang(rs.getString(4)));
zf.setTimes(rs.getString(5));
int chick=rs.getInt(6);
chicks(id,chick);
//list.add(zf);
} 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 zf;
}
public int insertZhutie(ZhutieForm zhutieForm) {
Date t = new Date();
String ts = t.toLocaleString();
int chicks=0;
int rs=0;
try {
ps = con
.prepareStatement("insert into zhutie(name, title, text, times, chicks) values(?,?,?,?,?)");
ps.setString(1, bx(zhutieForm.getName()));
ps.setString(2, bx(zhutieForm.getTitle()));
ps.setString(3, bx(zhutieForm.getText()));
ps.setString(4, ts);
ps.setInt(5, chicks);
rs=ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
rs=0;
}finally{
try {
ps.close();
stmt.close();
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return rs;
}
public int deleteZhutie(String id){
int rst=0;
try {
int ids=Integer.parseInt(id);
stmt=con.createStatement();
String sql="delete from zhutie 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 void chicks(int id,int chick){
try {
chick=chick+1;
stmt=con.createStatement();
String sql="update zhutie set chicks="+chick+" where id="+id;
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 + -