Productp=(Product)result.get(i);
xmlContent+=""+p.getName()+""
+p.getType()+""+p.getPrice()
+""+p.getNum()+"";
}
xmlContent+="";
out.print(xmlContent);
out.flush();
out.close();
}
}
/**
*ThedoPostmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto
*post.
*
*@paramrequest
*therequestsendbytheclienttotheserver
*@paramresponse
*theresponsesendbytheservertotheclient
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
this.doGet(request,response);
}
/**
*Initializationoftheservlet.
*
*@throwsServletException
*ifanerroroccurs
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
数据库连接
viewplaincopytoclipboardprint?
publicclassMyConnection{
publicConnectionconn=null;
publicMyConnection(){
try{
//注册数据库驱动程序为MYSQL驱动
Class.forName("com.mysql.jdbc.Driver");
}catch(java.lang.ClassNotFoundExceptione){
System.err.println("mydb():
"+e.getMessage());
}
try{
conn=DriverManager.getConnection(
"jdbc:
mysql:
//127.0.0.1:
3306/flex",
"root","root");
}catch(SQLExceptionex){
System.err.println("conn:
"+ex.getMessage());
}
}
publicConnectiongetDbConnection(){
returnconn;
}
}
publicclassMyConnection{
publicConnectionconn=null;
publicMyConnection(){
try{
//注册数据库驱动程序为MYSQL驱动
Class.forName("com.mysql.jdbc.Driver");
}catch(java.lang.ClassNotFoundExceptione){
System.err.println("mydb():
"+e.getMessage());
}
try{
conn=DriverManager.getConnection(
"jdbc:
mysql:
//127.0.0.1:
3306/flex",
"root","root");
}catch(SQLExceptionex){
System.err.println("conn:
"+ex.getMessage());
}
}
publicConnectiongetDbConnection(){
returnconn;
}
}
DAO
viewplaincopytoclipboardprint?
publicclassProductDao{
Connectionconn;
ResultSetrs;
Statementstmt;
publicProductDao(){
conn=newMyConnection().getDbConnection();
try{
stmt=conn.createStatement();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicListgetProduct(){
Listlist=newArrayList();
try{
Stringsql="select*fromproduct";
rs=stmt.executeQuery(sql);
while(rs.next()){
Stringname=rs.getString("name");
Stringtype=rs.getString("type");
doubleprice=Double.parseDouble(rs.getString("price"));
intnum=Integer.parseInt(rs.getString("num"));
Productp=newProduct(name,type,price,num);
list.add(p);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
returnlist;
}
}
}
publicclassProductDao{
Connectionconn;
ResultSetrs;
Statementstmt;
publicProductDao(){
conn=newMyConnection().getDbConnection();
try{
stmt=conn.createStatement();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicListgetProduct(){
Listlist=newArrayList();
try{
Stringsql="select*fromproduct";
rs=stmt.executeQuery(sql);
while(rs.next()){
Stringname=rs.getString("name");
Stringtype=rs.getString("type");
doubleprice=Double.parseDouble(rs.getString("price"));
intnum=Integer.parseInt(rs.getString("num"));
Productp=newProduct(name,type,price,num);
list.add(p);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
returnlist;
}
}
}
PODO
viewplaincopytoclipboardprint?
publicclassProduct{
privateStringname;
privateStringtype;
privatedoubleprice;
privateintnum;
publicProduct(Stringname,Stringtype,doubleprice,intnum){
this.name=name;
this.type=type;
this.price=price;
this.num=num;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetType(){
returntype;
}
publicvoidsetType(Stringtype){
this.type=type;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
publicintgetNum(){
returnnum;
}
publicvoidsetNum(intnum){
this.num=num;
}
}
publicclassProduct{
privateStringname;
privateStringtype;
privatedoubleprice;
privateintnum;
publicProduct(Stringname,Stringtype,doubleprice,intnum){
this.name=name;
this.type=type;
this.price=price;
this.num=num;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetType(){
returntype;
}
publicvoidsetType(Stringtype){
this.type=type;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
publicintge