public boolean addStudent(AddStudentForm studentForm) {
Connection conn = null;
PreparedStatement pstmt = null;
conn = DBConnection.getConnection();
String sql = "insert into Students values(4,?,?,?,to_date(?, 'yyyy-mm-dd'))"; try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentForm.getSname());
pstmt.setString(2, studentForm.getMajor());
pstmt.setFloat(3, studentForm.getScore());
pstmt.setDate(4, studentForm.getBirth());
int rowCount = pstmt.executeUpdate();
if (rowCount > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
/*create table STUDENT(
SID NUMBER(20) not null primary key,
sname varchar2(10),
major varchar2(10),
score float(10)
); */
alter table students add timeday date上面那个方法插入数据报错ORA-01858: a non-numeric character was found where a numeric was expected
但是我直接在数据库里面插入就可以
而且插入0012-12-12这个日期可以
1999-12-12就 不可以了
我发现了是插入日期的问题
请问是为什么?
Connection conn = null;
PreparedStatement pstmt = null;
conn = DBConnection.getConnection();
String sql = "insert into Students values(4,?,?,?,to_date(?, 'yyyy-mm-dd'))"; try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentForm.getSname());
pstmt.setString(2, studentForm.getMajor());
pstmt.setFloat(3, studentForm.getScore());
pstmt.setDate(4, studentForm.getBirth());
int rowCount = pstmt.executeUpdate();
if (rowCount > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
/*create table STUDENT(
SID NUMBER(20) not null primary key,
sname varchar2(10),
major varchar2(10),
score float(10)
); */
alter table students add timeday date上面那个方法插入数据报错ORA-01858: a non-numeric character was found where a numeric was expected
但是我直接在数据库里面插入就可以
而且插入0012-12-12这个日期可以
1999-12-12就 不可以了
我发现了是插入日期的问题
请问是为什么?
--------------------- ---------- ---------- ------------ -----------
1 s sd 100 0012-12-22
2 s sd 100 0022-12-12
3 se de 100 0012-12-12
5 dj lk 100 0011-11-22
4 sky english 100 0012-12-22
6 sky air 100 0011-11-11插入这样的birth就可以
插入1999-12-12这样就不行。我是服了难道中毒?
我这个是放到一个内网的机器上的
to_date(?, 'yyyy-mm-dd')
这里的应该是一个时间格式的字符串格式类似 '2009-08-29'但是你这里取用
pstmt.setDate(4, studentForm.getBirth());
给了一个日期类型,所以oracle处理会隐形的转型date为一个string,由于你的日期的默认格式
不是yyyy-mm-dd,而是其他的格式,所以?的格式转成了其他格式的字符串,
然后在转成yyyy-mm-dd就报你上面的错了,意思是格式不正确。