简体   繁体   English

在Java中使用SQL语句插入数字数据类型

[英]Inserting numeric data type using SQL statement in Java

I am writing a method which allows users to store their data within my database. 我正在编写一种允许用户将其数据存储在我的数据库中的方法。

The problem I have is storing a numeric datatype in my database, for a user's ID. 我的问题是将数字数据类型存储在数据库中以获取用户的ID。

Firstly, how can I tell if the ID number is auto-incrementing? 首先,如何确定ID号是否是自动递增的? These are it's properties: 这些是它的属性:

Type: Numeric
Column size: 4
Decimal digits: 0
Part of primary key: true
Part of an index: true
Position: 1

I'm sure I read somewhere that, setting it as part of an index (true) allows for auto-incrementation. 我确定我在某处读过,将其设置为索引(true)的一部分可以自动增加。 Can anyone confirm? 谁能确认?

More importantly, when inserting data into my table I receive an error stating: 更重要的是,将数据插入表时,我收到一条错误消息:

Columns of type 'NUMERIC' cannot hold values of type 'CHAR'. 

This is a snippet of my insert code (I can reveal more if need be): 这是我的插入代码的片段(如果需要,我可以透露更多内容):

Statement pStatement = conn.createStatement();
String selectSQL = ("INSERT INTO APP.PERSON VALUES ('" + '3' + "','" + user + "','" + pass + "','" + fName + "','" + sName + "','" + sQuestion + "','" + sAnswer + "') ");
pStatement.executeUpdate(selectSQL);

As you can see I am setting ID (the first value), to 3 manually. 如您所见,我正在将ID(第一个值)手动设置为3。 I believe this is throwing up the error and would like to know how to insert a numeric value, using the INSERT command. 我相信这会引发错误,并且想知道如何使用INSERT命令插入数字值。

I am coding in Java, using Netbeans 7.3 Beta 2, on Mac OS X Mountain Lion. 我在Mac OS X Mountain Lion上使用Netbeans 7.3 Beta 2在Java中进行编码。 I am using a Derby database. 我正在使用Derby数据库。

Hey bro I suggest u to use prepareStatement 嗨,兄弟,我建议您使用prepareStatement

String template = "INSERT INTO APP.PERSON VALUES (YOUR,TABLE,COLLUMN,NAME) values (?,?,?,?)";
           PreparedStatement stmt = yourConnection.prepareStatement(template);

So if your want to set your ID as an integer, let java do it with 因此,如果您想将ID设置为整数,请让Java使用

       stmt.setInt(1, ID);
       stmt.setString(2, user);
       stmt.setString(3, fName);
       stmt.executeUpdate();

I dont know how is your table structure but this is an example if you want to use int use stmt.setInt(1, 3); 我不知道您的表结构如何,但这是一个示例,如果您想使用int,请使用stmt.setInt(1, 3);

1--> position that u set up in string template 1->您在字符串模板中设置的位置

3--> maybe u want to hard coded the ID 3->也许您想对ID进行硬编码

here is my example pattern to use prepareStatement 这是我使用prepareStatement的示例模式

String name = "shadrach"
Double price = "100.00"
int qty = 3;
    String template = "INSERT INTO PRODUCT (NAME,PRICE,QTY) values (?,?,?)";
                PreparedStatement stmt = connection.prepareStatement(template);
                stmt.setString(1, name);
                stmt.setDouble(2, price);
                stmt.setInt(3, qty);
                stmt.executeUpdate();

You shouldn't put single quotes around the number. 您不应在数字周围加上单引号。 I'm not sure of the types of the other column data types but anything that is numeric should not contain the single quotes. 我不确定其他列数据类型的类型,但是任何数字形式都不应包含单引号。

Statement pStatement = conn.createStatement();

String selectSQL = ("INSERT INTO APP.PERSON VALUES (3,'" + user + "','" + pass + "','" + fName + "','" + sName + "','" + sQuestion + "','" + sAnswer + "') ");

 pStatement.executeUpdate(selectSQL);

You should also look into switching this up to use a PreparedStatement , concatenating Strings to execute SQL will leave you vulnerable to a SQL injection attack. 您还应该考虑将其切换为使用PreparedStatement ,将字符串连接起来执行SQL将使您容易受到SQL注入攻击的攻击。 Prepared statements will help mitigate this risk. 准备好的声明将有助于减轻这种风险。

You're inserting the character 3 when the table expects a number. 当表格需要数字时,您要插入字符 3。

You can tell if a column is auto-increment, using the ResultSetMetaData 's isAutoIncrement method. 您可以使用ResultSetMetaData的isAutoIncrement方法来判断列是否是自动递增的。 Hope that helps.... 希望有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么Java数据类型对应于Oracle SQL数据类型NUMERIC? - What Java data type corresponds to the Oracle SQL data type NUMERIC? 使用准备好的语句插入到数值字段 - Inserting to Numeric field using prepared statement 使用Java Netbeans将数据插入Sql - Inserting Data into Sql Using Java Netbeans Hive语句未使用Java类将数据插入表中 - Hive statement not inserting data into the table using java class 如何在插入数据之前在Java中测试SQL字段类型 - How to Test an SQL Field Type in Java Before Inserting Data 哪种Java数据类型对应于Oracle SQL数据类型NUMERIC / DECIMAL,并获得PLSQL的等效Java代码? - What Java data type corresponds to the Oracle SQL data type NUMERIC/DECIMAL and get equivalent Java code for PLSQL? 使用正确的数字数据类型 - Using the right numeric data type 将 SQL NUMERIC[10,0] 映射到 java 类型 - Mapping SQL NUMERIC[10,0] to java type 在Java中,将大double作为参数传递给sql server,从而导致“将数据类型varchar转换为数值时出错”。 - In Java, pass large double as parameter to sql server, causing “Error converting data type varchar to numeric.” (JTDS)java.sql.SQLException:将数据类型nvarchar转换为numeric时出错 - (JTDS) java.sql.SQLException: Error converting data type nvarchar to numeric
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM