java sql datetime_对于SQLserver中datetime类型数据的添加和读取

/** Created on 2007-7-16

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates*/packagedb;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.sql.Timestamp;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.List;/***@authorAdministrator

*

* TODO To change the template for this generated type comment go to Window -

* Preferences - Java - Code Style - Code Templates*/publicclassOpinionDAO

{publicintaddOpinion(Opinion opinion)

{

Connection con=null;

Statement st=null;//创建Statement对象ResultSet rs=null;try{

Class.forName("net.sourceforge.jtds.jdbc.Driver");

con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/ActiveBPEL","sa","peace");

st=con.createStatement();

String name=opinion.getName();

String department=opinion.getDepartment();

String receiveTime=opinion.getReceiveTime();

String dealTime=opinion.getDealTime();

String content=opinion.getContent();intcarInfoId=opinion.getCarInfoId();intactivityId=opinion.getActivityId();

st

.executeUpdate("insert  opinion(name,department,receiveTime,dealTime,content,carInfoId,activityId) values("+"'"+name+"','"+department+"','"+receiveTime+"','"+dealTime+"','"+content+"',"+carInfoId+","+activityId+")");

rs=st.executeQuery("select max(opinionId) from opinion");intopinionId=-1;while(rs.next())

{

opinionId=rs.getInt(1);

}returnopinionId;

}catch(Exception e)

{//TODO Auto-generated catch blocke.printStackTrace();

}finally{if(rs!=null)

{try{

rs.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

rs=null;

}if(st!=null)

{try{

st.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

st=null;

}if(con!=null)

{try{

con.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

con=null;

}

}return-1;

}publicvoidupdateOpinion(Opinion opinion)

{

Connection con=null;

Statement st=null;//创建Statement对象try{

Class.forName("net.sourceforge.jtds.jdbc.Driver");

con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/ActiveBPEL","sa","peace");

st=con.createStatement();intopinionId=opinion.getOpinionId();

String name=opinion.getName();

String department=opinion.getDepartment();

String receiveTime=opinion.getReceiveTime();

String dealTime=opinion.getDealTime();

String content=opinion.getContent();intcarInfoId=opinion.getCarInfoId();intactivityId=opinion.getActivityId();

st

.executeUpdate("update  opinion set name="+"'"+name+"',department='"+department+"',receiveTime='"+receiveTime+"',dealTime='"+dealTime+"',content='"+content+"',carInfoId="+carInfoId+",activityId="+activityId+"where opinionId="+opinionId);//输出被查询的情况}catch(Exception e)

{//TODO Auto-generated catch blocke.printStackTrace();

}finally{if(st!=null)

{try{

st.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

st=null;

}if(con!=null)

{try{

con.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

con=null;

}

}

}publicList getOpinions(intcarInfoId)

{

Connection con=null;

Statement st=null;//创建Statement对象ResultSet rs=null;

List opinions=newArrayList();try{

Class.forName("net.sourceforge.jtds.jdbc.Driver");

con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/ActiveBPEL","sa","peace");

st=con.createStatement();

rs=st.executeQuery("select * from opinion where carInfoId="+carInfoId);while(rs.next())

{

Opinion opinion=newOpinion();

opinion.setOpinionId(rs.getInt("opinionId"));

opinion.setName(rs.getString("name"));

opinion.setDepartment(rs.getString("department"));

Timestamp timee=rs.getTimestamp("receiveTime");

SimpleDateFormat myFormat=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String mystrdate="";if(timee!=null)

{

mystrdate=myFormat.format(timee);

}

opinion.setReceiveTime(mystrdate);

timee=rs.getTimestamp("dealTime");if(timee!=null)

{

mystrdate=myFormat.format(timee);

}

opinion.setDealTime(mystrdate);

opinion.setContent(rs.getString("content"));

opinion.setCarInfoId(rs.getInt("carInfoId"));

opinion.setActivityId(rs.getInt("activityId"));

opinions.add(opinion);

}returnopinions;//输出被查询的情况}catch(Exception e)

{//TODO Auto-generated catch blocke.printStackTrace();

}finally{if(rs!=null)

{try{

rs.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

rs=null;

}if(st!=null)

{try{

st.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

st=null;

}if(con!=null)

{try{

con.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

con=null;

}

}returnnull;

}publicOpinion getOpinion(intcarInfoId,intactivityId)

{

Connection con=null;

Statement st=null;//创建Statement对象ResultSet rs=null;

Opinion opinion=newOpinion();try{

Class.forName("net.sourceforge.jtds.jdbc.Driver");

con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/ActiveBPEL","sa","peace");

st=con.createStatement();

rs=st.executeQuery("select opinionId,content from opinion where carInfoId="+carInfoId+"and activityId="+activityId);while(rs.next())

{

opinion.setOpinionId(rs.getInt("opinionId"));

opinion.setContent(rs.getString("content"));

}returnopinion;//输出被查询的情况}catch(Exception e)

{//TODO Auto-generated catch blocke.printStackTrace();

}finally{if(rs!=null)

{try{

rs.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

rs=null;

}if(st!=null)

{try{

st.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

st=null;

}if(con!=null)

{try{

con.close();

}catch(SQLException e1)

{//TODO Auto-generated catch blocke1.printStackTrace();

}

con=null;

}

}returnopinion;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值