package com.shangyu.action;
import com.opensymphony.xwork2.ActionSupport;
import java.sql. * ;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.shangyu.db. * ;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class HelloWorld extends ActionSupport ... {
List userList=null;
private String username="";
private String userpwd="";
public List getUserList() ...{
return userList;
}
public void setUserList(List userList) ...{
this.userList = userList;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getUserpwd() ...{
return userpwd;
}
public void setUserpwd(String userpwd) ...{
this.userpwd = userpwd;
}
public String execute()
...{
/**//*传统的通过jdbc来进行数据库的操作
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try
{
conn=ProxoolAction.getConnection();
stmt=conn.prepareStatement("select * from 资源预约表");
rs=stmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(2));
}
}catch(Exception e)
{
System.out.println(e);
}finally
{
ProxoolAction.closeResultSet(rs);
ProxoolAction.closeStatement(stmt);
ProxoolAction.closeConnection(conn);
}
System.out.println(getUsername()+" "+getUserpwd());
*/
final List resourceList= new ArrayList();
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource ds =(DataSource) ctx.getBean("TestSource");
JdbcTemplate jdbcTemplate=new JdbcTemplate(ds);
//直接利用sql语句来插入数据
//String sqlstr="insert into 资源预约表(r_id,reg_time,reg_xq,reg_week,reg_userxm) values(1,'2008-04-05','2007-2008',1,'admin')";
//带参数的方式向数据库中插入数据
//String sqlstr="insert into 资源预约表(r_id,reg_time,reg_xq,reg_week,reg_userxm) values(?,?,?,?,?)";
//Object[] params=new Object[]{1,"2008-04-25","2007-2008",1,"shangyu79"};
//jdbcTemplate.update(sqlstr,params);
//修改数据
String sqlstr="update 资源预约表 set r_id=? where reg_id=?";
Object[] params=new Object[]...{1000,24};
jdbcTemplate.update(sqlstr,params);
//得到表中记录数
sqlstr="select count(1) from 资源预约表";
int i=jdbcTemplate.queryForInt(sqlstr);
System.out.println("资源预约表总数据:"+i);
jdbcTemplate.query("select * from 资源预约表", new RowCallbackHandler()...{
public void processRow(ResultSet rs) throws SQLException...{
Map user=new HashMap();
user.put("username", rs.getString("reg_content"));
user.put("xq",rs.getString("reg_xq"));
resourceList.add(user);
}
});
/**//*Iterator it= resourceList.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}*/
setUserList(resourceList);
return SUCCESS;
}
}
import com.opensymphony.xwork2.ActionSupport;
import java.sql. * ;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.shangyu.db. * ;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class HelloWorld extends ActionSupport ... {
List userList=null;
private String username="";
private String userpwd="";
public List getUserList() ...{
return userList;
}
public void setUserList(List userList) ...{
this.userList = userList;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getUserpwd() ...{
return userpwd;
}
public void setUserpwd(String userpwd) ...{
this.userpwd = userpwd;
}
public String execute()
...{
/**//*传统的通过jdbc来进行数据库的操作
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try
{
conn=ProxoolAction.getConnection();
stmt=conn.prepareStatement("select * from 资源预约表");
rs=stmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(2));
}
}catch(Exception e)
{
System.out.println(e);
}finally
{
ProxoolAction.closeResultSet(rs);
ProxoolAction.closeStatement(stmt);
ProxoolAction.closeConnection(conn);
}
System.out.println(getUsername()+" "+getUserpwd());
*/
final List resourceList= new ArrayList();
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource ds =(DataSource) ctx.getBean("TestSource");
JdbcTemplate jdbcTemplate=new JdbcTemplate(ds);
//直接利用sql语句来插入数据
//String sqlstr="insert into 资源预约表(r_id,reg_time,reg_xq,reg_week,reg_userxm) values(1,'2008-04-05','2007-2008',1,'admin')";
//带参数的方式向数据库中插入数据
//String sqlstr="insert into 资源预约表(r_id,reg_time,reg_xq,reg_week,reg_userxm) values(?,?,?,?,?)";
//Object[] params=new Object[]{1,"2008-04-25","2007-2008",1,"shangyu79"};
//jdbcTemplate.update(sqlstr,params);
//修改数据
String sqlstr="update 资源预约表 set r_id=? where reg_id=?";
Object[] params=new Object[]...{1000,24};
jdbcTemplate.update(sqlstr,params);
//得到表中记录数
sqlstr="select count(1) from 资源预约表";
int i=jdbcTemplate.queryForInt(sqlstr);
System.out.println("资源预约表总数据:"+i);
jdbcTemplate.query("select * from 资源预约表", new RowCallbackHandler()...{
public void processRow(ResultSet rs) throws SQLException...{
Map user=new HashMap();
user.put("username", rs.getString("reg_content"));
user.put("xq",rs.getString("reg_xq"));
resourceList.add(user);
}
});
/**//*Iterator it= resourceList.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}*/
setUserList(resourceList);
return SUCCESS;
}
}