Skip to content

Instantly share code, notes, and snippets.

@wjch
Last active December 15, 2015 07:49
Show Gist options
  • Save wjch/5226427 to your computer and use it in GitHub Desktop.
Save wjch/5226427 to your computer and use it in GitHub Desktop.
<!-- action里使用HttpServletRequest request = ServletActionContext. getRequest();
request.setAttribute("list", list);传值 -->
<s:iterator value="#request.list" status="stu">
<div class="span4">
<a href="" title="%{sname}" target="_blank"><img class="marketing-img" src="<%=imgpath %><s:property value="%{simg}"/>"></a>
<h2>
<a href="" title="<s:property value="%{susername}"/>" target="_blank"><s:property value="%{susername}"/><br><small><s:property value="%{spost}"/></small></a>
</h2>
<p>
<s:property value="%{sdesc}"/>
</p>
</div>
</s:iterator>
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.wjch.util.Datasource;
public class TeaDao {
Datasource ds =new Datasource();
public void selectTeacher(){
Connection conn=null;
try {
conn = Datasource.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
PreparedStatement ps =null;
ResultSet rs =null;
try {
String sql="SELECT t_name,t_post,t_desc,t_img FROM b_stu";
ps= conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
Student stu =new Student();
stu.setSusername(rs.getString(1));
stu.setSpost(rs.getString(2));
stu.setSdesc(rs.getString(3));
stu.setSimg(rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Teacher> getSTeachers(){
Connection conn=null;
List<Teacher> list = new ArrayList<Teacher>();
try {
conn = Datasource.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
PreparedStatement ps =null;
ResultSet rs =null;
try {
String sql="SELECT t_name,t_post,t_desc,t_img FROM b_tea";
ps= conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY );
rs=ps.executeQuery();
int i=1;
rs.beforeFirst();
//这里犯错了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//不是if 而是while
while(rs.next()){
Teacher tea = new Teacher();
tea.setTusername(rs.getString(1));
tea.setTpost(rs.getString(2));
tea.setTdesc(rs.getString(3));
tea.setTimg(rs.getString(4));
System.out.println(tea.getTusername());
list.add(tea);
i++;
}
System.out.println(i);
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public void insertTeachers(String username,String post,String desc,String img){
Connection conn=null;
try {
conn = Datasource.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
PreparedStatement ps =null;
try {
String sql="INSERT INTO b_tea(t_name, t_post,t_desc, t_img)VALUES(?,?,?,?)";
ps= conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, post);
ps.setString(3, desc);
ps.setString(4, img);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void deleteTeacher(String username){
Connection conn=null;
try {
conn = Datasource.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
PreparedStatement ps =null;
try {
String sql="DELETE FROM b_stu where s_name=?";
ps= conn.prepareStatement(sql);
ps.setString(1, username);
ps.executeUpdate();
System.out.println("删除成功");
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void updateTeacher(String username,String post,String desc,String img){
Connection conn=null;
try {
conn = Datasource.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
PreparedStatement ps =null;
ResultSet rs =null;
try {
String sql="UPDATE b_tea SET t_name = ?,t_post = ?,t_desc = ?,t_img =? WHERE t_name =?";
ps=conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, post);
ps.setString(3, desc);
ps.setString(4, img);
ps.setString(5, username);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment