oracle ORA-01840:输入值对于日期格式不够长,请高手帮忙。有非法数据?怎么处理?
日期型数据内有不规范数据造成的。找出那条记录,UPDATE日期字段。
其具体的语法格式如下:
TRUNC(date,[fmt])
其中:
date 为必要参数,是输入的一个日期值
fmt 参数可忽略,是日期格式,用以指定的元素格式来截去输入的日期值。忽略它则由最近的日期截去
下面是该函数的使用情况:
trunc(sysdate,'yyyy') --返回当年第一天.
trunc(sysdate,'mm') --返回当月第一天.
trunc(sysdate,'d') --返回当前星期的第一天.
public class test1 extends JFrame implements ActionListener{
JPanel jp1,jp2;
JTable jt1=null;
JLabel jl1=null;
JTextField jtf=null;
JButton jb1,jb2,jb3,jb4;
JTable jt=null;
JScrollPane jsp=null;
//String sql;
public static void main(String[] args) {
test1 t1=new test1();
}
test1(){
jp1=new JPanel();
jl1=new JLabel("请输入要查询的编号");
jtf=new JTextField(10);
jb1=new JButton("查询");
jb1.addActionListener(this);
jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);
jp2=new JPanel();
jb2=new JButton("添加");
jb3=new JButton("删除");
jb4=new JButton("修改");
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
tablemodel t=new tablemodel();
jt1=new JTable(t);
jsp=new JScrollPane(jt1);
this.add(jsp);
this.add(jp1,"North");
this.add(jp2,"South");
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==jb1){
//System.out.println("王博古");
String name=this.jtf.getText().trim();
String sql="select * from lixianqi where name="+"'"+name+"'";
tablemodel t=new tablemodel(sql);
jt.setModel(t);
}
}
}
Vector rowData, columnNames;
Connection connection=null;
PreparedStatement ps=null;
ResultSet res=null;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public tablemodel(){
columnNames=new Vector();
columnNames.add("编码");
columnNames.add("版本");
columnNames.add("名字");
rowData=new Vector();
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connection=DriverManager.getConnection
("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=zhudan","sa","zhudan");
ps=connection.prepareStatement("select * from lixianqi");
res=ps.executeQuery();
while(res.next()){
Vector v=new Vector();
v.add(res.getInt(1));
v.add(res.getString(2));
v.add(res.getString(3));
rowData.add(v);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(res!=null)res.close();
if(ps!=null)ps.close();
if(connection!=null)connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public tablemodel(String sql){
columnNames=new Vector();
columnNames.add("编码");
columnNames.add("版本");
columnNames.add("名字");
rowData=new Vector();
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connection=DriverManager.getConnection
("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=zhudan","sa","zhudan");
ps=connection.prepareStatement(sql);
res=ps.executeQuery();
while(res.next()){
Vector ve=new Vector();
ve.add(res.getInt(1));
ve.add(res.getString(2));
ve.add(res.getString(3));
rowData.add(ve);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(res!=null)res.close();
if(ps!=null)ps.close();
if(connection!=null)connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//得到多少列
public int getColumnCount() {
System.out.println("a");
return this.columnNames.size();
}
//得到多少列
public int getRowCount() {
System.out.println("2");
return this.rowData.size();
}
//得到哪行哪列
public Object getValueAt(int row, int colum) {
System.out.println("3");
return ((Vector)this.rowData.get(row)).get(colum);
}
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String) columnNames.get(column);
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at chack.test1.actionPerformed(test1.java:75)
其具体的语法格式如下:
TRUNC(date,[fmt])
其中:
date 为必要参数,是输入的一个日期值
fmt 参数可忽略,是日期格式,用以指定的元素格式来截去输入的日期值。忽略它则由最近的日期截去
下面是该函数的使用情况:
trunc(sysdate,'yyyy') --返回当年第一天.
trunc(sysdate,'mm') --返回当月第一天.
trunc(sysdate,'d') --返回当前星期的第一天.说明 你要输入的日期过短 ,可能要求输入日期的格式是‘yyyy-mm-dd hh24:mi:ss'
你就要输入 20130101125959 日期就变成 2013年1月1日12点59分59秒
如果你要看自己输入的结果 输入:
select to_char(to_date($rundate,'yyyymmddhh24miss’),‘yyyy-mm-dd hh24:mi:ss') from dual
说明 你要输入的日期过短 ,可能要求输入日期的格式是‘yyyy-mm-dd hh24:mi:ss'
你就要输入 20130101125959 日期就变成 2013年1月1日12点59分59秒
如果你要看自己输入的结果 输入:
select to_char(to_date($rundate,'yyyymmddhh24miss’),‘yyyy-mm-dd hh24:mi:ss') from dual;
希望对你有帮助。
正确语句
select to_date('2012-05-06 12:05:03','yyyy-mm-dd hh24:mi:ss') from dual
select to_date('2012-05-06','yyyy-mm-dd hh24:mi:ss') from dual
select to_date('2012-05-06','yyyy-mm-dd') from dual
报错语句
select to_date('2012-05-06 12:05:03','yyyy-mm-dd') from dual
自己想去吧
可能有几种原因
1、转换格式有问题;yyyy-mm-dd hh24:mi:ss或者yyyy-mm-dd hh:mi:ss
2、转换的字段里面有空值
把你要输入的日期发过来
相关视频