62,635
社区成员




import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
public class EmpDB{
//私有属性
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
public void saveEmpInfo(){
try {
conn=DBManager.getConnection();
String sql="insert into employee values(?,?,?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"女");//sex,age,name,address
pstmt.setInt(3,18);
pstmt.setString(2,"郭美梅");
pstmt.setString(4,"北京京西胡同");
pstmt.setDate(5,new Date(new java.util.Date().getTime()));
int result=pstmt.executeUpdate();
if(result>0)
System.out.println("增加数据成功!");
else
System.out.println("增加数据失败!");
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(rs, pstmt, conn);
}
}
//更新数据的方法;
public void updateEmpInfo(){
try {
conn=DBManager.getConnection();
String sql="update employee set name=?,age=? date=? where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"马云峰");
pstmt.setInt(2,28);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String date2="2011-10-12";
java.util.Date d2=sdf.parse(date2);
pstmt.setDate(3,new Date(d2.getDate()));
//setDate(3,new java.sql.date())
pstmt.setInt(4,6);
int result=pstmt.executeUpdate();
if(result>0)
System.out.println("ok");
else
System.out.println("failue");
} catch (SQLException e) {
e.printStackTrace();
}catch(Exception ex){}
finally{
DBManager.closeAll(rs, pstmt, conn);
}
}
}
com.microsoft.sqlserver.jdbc.SQLServerException: 'date' 附近有语法错误。