英文:
ORA-01722 error while executing PreparedStatement
问题
这是代理商表的代码,它正常工作。
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.31.70.51:1521:TSTSLSDB", "BIOSIGN", "BIOSIGN123*");
String sqlQuery = "INSERT INTO DEALER (ID, NAME, DEALER_CODE, BRANCH_CODE, ADDRESS, PHONE_NUMBER, TAX_OFFICE_NAME, TAX_NUMBER, EMAIL, CITY_NAME)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?)";
PreparedStatement prepstmt = conn.prepareStatement(sqlQuery);
ThreadLocalRandom random = ThreadLocalRandom.current();
long x = random.nextLong(10_000_000_000L, 100_000_000_000L);
prepstmt.setLong(1, x);
prepstmt.setString(2, "HACI");
prepstmt.setString(3, "K000003");
prepstmt.setString(4, "01");
prepstmt.setString(5, "TARLABAŞI Çilek Sokak");
prepstmt.setString(6, "2482333283");
prepstmt.setNull(7, java.sql.Types.NULL);
prepstmt.setNull(8, java.sql.Types.NULL);
prepstmt.setNull(9, java.sql.Types.NULL);
prepstmt.setNull(10, java.sql.Types.NULL);
prepstmt.execute();
PreparedStatement prepstmt2 = conn.prepareStatement("SELECT * from dealer where id=?");
prepstmt2.setLong(1, x);
prepstmt2.execute();
我正在对 MERNIS_CUSTOMER_INFO 表使用相同的方法,但在执行 prepstmt1.execute()
时出现 ORA-01722 错误。有什么区别吗?我找不出问题所在。我只是尝试读取通过 prepstmt
插入的记录。
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.31.60.83:1521:TBCEBTRL", "TESTUSER", "TESTUSER");
String sqlQuery = "INSERT INTO PROVISIONING_MGNT.MERNIS_CUSTOMER_INFO (TC_ID, MOTHER_TC_ID, FATHER_TC_ID, BIRTHPLACE_CODE, SPOUSE_TC_ID, NAME, SURNAME, SEX, FATHER_NAME, MOTHER_NAME, DATE_OF_BIRTH_YEAR, DATE_OF_BIRTH_MONTH, DATE_OF_BIRTH_DAY, PLACE_OF_BIRTH, STATUS, MARITAL_STATUS, RELIGION, DATE_OF_DEATH_YEAR, DATE_OF_DEATH_MONTH, DATE_OF_DEATH_DAY, PROVINCE_CODE, PROVINCE_NAME, COUNTY_CODE, COUNTY_NAME, VOLUME_CODE, VOLUME_NAME, FAMILY_SEQUENCE_NUMBER, PERSON_SEQUENCE_NUMBER, ERROR_CODE, ERROR_DESCRIPTION, CREATION_TIME, DOCUMENT_NO)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement prepstmt = conn.prepareStatement(sqlQuery);
Random r = new Random();
int number = 1000000000 + (int)(r.nextDouble() * 999999999);
ThreadLocalRandom random = ThreadLocalRandom.current();
long x = random.nextLong(10_000_000_000L, 100_000_000_000L);
System.out.println(x);
prepstmt.setLong(1, x);
prepstmt.setNull(2, java.sql.Types.NULL);
prepstmt.setNull(3, java.sql.Types.NULL);
prepstmt.setInt(4, 1542);
prepstmt.setNull(5, java.sql.Types.NULL);
prepstmt.setString(6, "TESTxx");
prepstmt.setString(7, "TEST");
prepstmt.setString(8, "Male");
prepstmt.setString(9, "FATHER");
prepstmt.setString(10, "Mother");
prepstmt.setInt(11, 1968);
prepstmt.setInt(12, 10);
prepstmt.setInt(13, 10);
prepstmt.setString(14, "ADANA");
prepstmt.setString(15, "Open");
prepstmt.setString(16, "Single");
prepstmt.setNull(17, java.sql.Types.NULL);
prepstmt.setNull(18, java.sql.Types.NULL);
prepstmt.setNull(19, java.sql.Types.NULL);
prepstmt.setNull(20, java.sql.Types.NULL);
prepstmt.setInt(21, 1);
prepstmt.setString(22, "ADANA");
prepstmt.setInt(23, 1101);
prepstmt.setString(24, "TEST");
prepstmt.setInt(25, 1);
prepstmt.setString(26, "MERKEZ");
prepstmt.setInt(27, 39);
prepstmt.setInt(28, 2);
prepstmt.setNull(29, java.sql.Types.NULL);
prepstmt.setNull(30, java.sql.Types.NULL);
prepstmt.setNull(31, java.sql.Types.NULL);
prepstmt.setInt(32, number);
prepstmt.execute();
PreparedStatement prepstmt1 = conn.prepareStatement("SELECT * from PROVISIONING_MGNT.MERNIS_CUSTOMER_INFO where TC_ID=?");
prepstmt1.setLong(1, x);
prepstmt1.execute();
英文:
this is the code for dealer table and it works correctly.
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.31.70.51:1521:TSTSLSDB", "BIOSIGN","BIOSIGN123*");
String sqlQuery = "INSERT INTO DEALER (ID,NAME,DEALER_CODE,BRANCH_CODE,ADDRESS,PHONE_NUMBER,TAX_OFFICE_NAME,TAX_NUMBER,EMAIL,CITY_NAME)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?)";
PreparedStatement prepstmt = conn.prepareStatement(sqlQuery);
ThreadLocalRandom random = ThreadLocalRandom.current();
long x = random.nextLong(10_000_000_000L, 100_000_000_000L);
prepstmt.setLong(1,x);
prepstmt.setString(2,"HACI");
prepstmt.setString(3,"K000003");
prepstmt.setString(4,"01");
prepstmt.setString(5,"TARLABAŞI Çilek Sokak");
prepstmt.setString(6,"2482333283");
prepstmt.setNull(7,java.sql.Types.NULL );
prepstmt.setNull(8,java.sql.Types.NULL );
prepstmt.setNull(9,java.sql.Types.NULL );
prepstmt.setNull(10,java.sql.Types.NULL );
prepstmt.execute();
PreparedStatement prepstmt2 =conn.prepareStatement("SELECT * from dealer where id=?");
prepstmt2.setLong(1,x);
prepstmt2.execute();
and i'm applying the same approach for MERNIS_CUSTOMER_INFO table but ORA-01722 error occurs while being executed prepstmt1.execute(). What's the difference ? I'm not able to find out the problem. I just try to read the record which is inserted through prepstmt.
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.31.60.83:1521:TBCEBTRL", "TESTUSER","TESTUSER");
String sqlQuery = "INSERT INTO PROVISIONING_MGNT.MERNIS_CUSTOMER_INFO (TC_ID,MOTHER_TC_ID,FATHER_TC_ID,BIRTHPLACE_CODE,SPOUSE_TC_ID,NAME,SURNAME,SEX,FATHER_NAME,MOTHER_NAME,DATE_OF_BIRTH_YEAR,DATE_OF_BIRTH_MONTH,DATE_OF_BIRTH_DAY,PLACE_OF_BIRTH,STATUS,MARITAL_STATUS,RELIGION,DATE_OF_DEATH_YEAR,DATE_OF_DEATH_MONTH,DATE_OF_DEATH_DAY,PROVINCE_CODE,PROVINCE_NAME,COUNTY_CODE,COUNTY_NAME,VOLUME_CODE,VOLUME_NAME,FAMILY_SEQUENCE_NUMBER,PERSON_SEQUENCE_NUMBER,ERROR_CODE,ERROR_DESCRIPTION,CREATION_TIME,DOCUMENT_NO)"
+"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement prepstmt = conn.prepareStatement(sqlQuery);
// Statement prepstmt = conn.createStatement();
Random r = new Random();
int number = 1000000000 + (int)(r.nextDouble() * 999999999);
ThreadLocalRandom random = ThreadLocalRandom.current();
long x = random.nextLong(10_000_000_000L, 100_000_000_000L);
System.out.println(x);
prepstmt.setLong(1,x);
prepstmt.setNull(2,java.sql.Types.NULL );
prepstmt.setNull(3,java.sql.Types.NULL );
prepstmt.setInt(4,1542);
prepstmt.setNull(5,java.sql.Types.NULL );
prepstmt.setString(6,"TESTxx");
prepstmt.setString(7,"TEST");
prepstmt.setString(8,"Male");
prepstmt.setString(9,"FATHER");
prepstmt.setString(10,"Mother");
prepstmt.setInt(11,1968);
prepstmt.setInt(12,10);
prepstmt.setInt(13,10);
prepstmt.setString(14,"ADANA");
prepstmt.setString(15,"Open");
prepstmt.setString(16,"Single");
prepstmt.setNull(17,java.sql.Types.NULL );
prepstmt.setNull(18,java.sql.Types.NULL );
prepstmt.setNull(19,java.sql.Types.NULL );
prepstmt.setNull(20,java.sql.Types.NULL );
prepstmt.setInt(21,1);
prepstmt.setString(22,"ADANA");
prepstmt.setInt(23,1101);
prepstmt.setString(24,"TEST");
prepstmt.setInt(25,1);
prepstmt.setString(26,"MERKEZ");
prepstmt.setInt(27,39);
prepstmt.setInt(28,2);
prepstmt.setNull(29,java.sql.Types.NULL );
prepstmt.setNull(30,java.sql.Types.NULL );
prepstmt.setNull(31,java.sql.Types.NULL );
prepstmt.setInt(32,number);
prepstmt.execute();
PreparedStatement prepstmt1 =conn.prepareStatement("SELECT * from PROVISIONING_MGNT.MERNIS_CUSTOMER_INFO where TC_ID=?");
prepstmt1.setLong(1,x);
prepstmt1.execute();
答案1
得分: 1
ORA-01722
发生在执行 SQL 语句时,尝试将一个字符串转换为数字,但失败了。
基本上,可能是现有数据中出现了问题,而不是你粘贴的代码有问题。执行以下查询可以帮助你识别问题所在:
SELECT *
FROM PROVISIONING_MGNT.MERNIS_CUSTOMER_INFO
WHERE NOT REGEXP_LIKE(TC_ID, '^-?[0-9]+(\.[0-9]+)?$');
解决这个错误的理想方法是在后端和数据库层之间对齐数据类型。
英文:
ORA-01722
occurs when you executed a SQL statement that tried to convert a string to a number, but it was unsuccessful.
Essentially it can be something wrong in existing data rather than your code pasted. Executing below query against TC_ID
can help you identify the culprit.
SELECT *
FROM PROVISIONING_MGNT.MERNIS_CUSTOMER_INFO
WHERE NOT REGEXP_LIKE(TC_ID, '^-?[0-9]+(\.[0-9]+)?$');
Ideal way to resolve this error is to align data types across backend and database layer.
答案2
得分: 0
错误代码ORA-01722
表示无效的数字
。
提示:
当您尝试将字符串转换为数字时,会出现此错误。您可能在数据库中的数字字段上使用了setString
。
如果没有提供您的表定义,很难确定问题出在哪里。
英文:
Error code ORA-01722
means invalid number
Tips:
This error occurs when you try to convert a String to a Number. You probably make a setString
to a Number field in your database.
Without your table definition, it's difficult to say where the problem is exactly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论