[英]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.