当我尝试通过eclipse上传我的Books表时,我不断收到以下错误消息:

java.sql.SQLSyntaxErrorException: ORA-01722: invalid number


我已经能够使用此基本代码上载数据库中的其他表,而不仅仅是这个。
我认为这可能与我上传日期的方式有关,但我不确定。

package uploadDatabase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import oracle.jdbc.OracleDriver;
import java.sql.DatabaseMetaData;
import java.sql.Date;


public class StatesTable {

    public static void main(String[] args) {


        //private static Connection connection;


        try{

            DriverManager.registerDriver(new OracleDriver());

            //make strings for database connections
            String url = "jdbc:oracle:thin:@localhost:1521:xe";
            String userName = "BOOKSTORE";
            String password = "***********";

            //make database connection
            Connection conn =               DriverManager.getConnection(url,userName,password);

            DatabaseMetaData meta = conn.getMetaData();

            System.out.println(meta.getDatabaseProductVersion());

            String isbn[] =    {"00000000110","00000000111","00000000112","00000000113","00000000114"};

            String title[] = {"THE SHINING", "THE GIRL WITH THE DRAGON TATTO", "PRIDE AND PREJUDICE", "BOSSYPANTS", "THE HUNGER GAMES"};

            String author[] = {"STEPHEN KING", "STIEG LARSSON", "JANE AUSTEN", "TINA FEY", "SUZANNE COLLINS"};

            String publishDate[] ={"19750115","19990805","18731015","20160105","19821115"};

            String edition[] = {"6TH","3RD","26TH","1ST","7TH"};

            double cost[] = {15.75,17.95,8.95,9.95,12.95};

            String genre[] = {"HORROR", "MYSTERY", "ROMANCE","COMEDY", "ACTION"};



            //Database statement for inserting values into BOOKS table
            String sqlStatement = "INSERT INTO BOOKS VALUES(?,?,?,?,?,?,?)";

            //Prepared statement for database connection
            PreparedStatement pstmt = conn.prepareStatement(sqlStatement);

            //loop uploads data into database;
            for(int i = 0; i < 5; i++){
                //insert values into dbms statement
                  String isb = isbn[i];
                  String tit = title[i];
                  String auth = author[i];
                  SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                  java.util.Date parsed = df.parse(publishDate[i]);
                  Date publishDat = new Date(parsed.getTime());
                  System.out.println(publishDat);
                  String editio = edition[i];
                  double cos = cost[i];
                  String gen = genre[i];

                  pstmt.setString(1,isb);
                  pstmt.setString(2,tit);
                  pstmt.setString(3,auth);
                  pstmt.setDate(4,publishDat);
                  pstmt.setString(5,editio);
                  pstmt.setDouble(6, cos);
                  pstmt.setString(7, gen);


                //Execute update
                pstmt.executeUpdate();

            }

             //close pstmt statement
            pstmt.close();
            //close database connection
            conn.close();



        }
            catch(SQLException e){
                e.printStackTrace();
            } catch (ParseException e) {
             e.printStackTrace();
            }

        }
    }

最佳答案

出现此错误的原因是,您传递给insert命令的一种列数据类型与实际定义的数据类型不同。确保检查表及其列值和数据类型,并确保这些数据类型与您要插入该数据库的数据类型对齐。就像Andreas在下面的评论中所说的那样,您应该始终在INSERT语句中命名列,没有它们,您将不知道数据库中将包含什么内容。希望这可以帮助 :)

关于java - 将日期的字符串数组上载到oracle,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41405393/

10-11 10:55