您的位置:首页 > 编程语言

虽是自己写的代码,但还是不是很理解

2010-04-25 23:40 477 查看
package myBBS;
import java.util.*;
import java.util.Date;
import java.sql.*;
import java.text.*;

public class postDBOpreation {

public List getpostnews()
{
List list=new ArrayList(); //动态数组
Connection dbConnection=null; //创建一个连接变量
PreparedStatement pStatement=null; //创建一个注入SQL语句的对象
ResultSet res=null; //结果集

try
{
dbConnection=ConnectionManager.getConnection(); //生成连接变量
String str="select * from postnews order by CreateTime desc";
pStatement=dbConnection.prepareStatement(str); //生成一个注入语句
res=pStatement.executeQuery(); //执行注入语句,并将结果赋值
while(res.next())
{
int ID=res.getInt("ID");
String Titlename=res.getString("Titlename");
String File=res.getString("File");
String Creator=res.getString("Creator");
Date Createtime=res.getDate("Createtime");
//将数据分离取出,赋给四个变量
postnews p=new postnews(ID,Titlename,File,Creator,Createtime);
list.add(p);
//数据全部存储到了list中
}

}
catch(SQLException SQLE)
{
SQLE.printStackTrace();
}
finally
{
ConnectionManager.closeConnection(dbConnection);
ConnectionManager.closeStatement(pStatement);
ConnectionManager.closeResultSet(res);
}
return list;
}
//读取数据

public int insetnews(postnews p)
{
int result=0;

Connection dbConnection=null; //创建一个连接变量
PreparedStatement pStatement=null; //创建一个注入SQL语句的对象

try
{

SimpleDateFormat sd=new SimpleDateFormat("yy-MM-dd");
String strtime=sd.format(new Date());

dbConnection=ConnectionManager.getConnection(); //生成连接变量
String str="insert into postnews values(?,?,?,?,?)";
pStatement=dbConnection.prepareStatement(str); //生成一个注入语句
pStatement.setInt(1,p.getID());
pStatement.setString(2,p.getTitlename());
pStatement.setString(3,p.getFile());
pStatement.setString(4,p.getCreator());
pStatement.setString(5, strtime);
result=pStatement.executeUpdate();
}
catch(SQLException SQLE)
{
SQLE.printStackTrace();
}
finally
{
ConnectionManager.closeConnection(dbConnection);
ConnectionManager.closeStatement(pStatement);
}
return result;

}
//插入数据
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐