Java 如何将数据添加到数据库表的新行中

Java 如何将数据添加到数据库表的新行中,java,database,derby,Java,Database,Derby,我使用的是netbeans版本7.3.1,我只想在netbeans GUI中获取用户输入到多个文本字段中的数据,然后获取该数据并使用它在表中创建新行,在数据库中,我使用它创建新用户。我正在使用derby嵌入式 您应该使用(JDBC)将Java代码连接到数据库 SQL命令是: INSERT INTO table_name VALUES (value1,value2,value3,...); 使用Java和JDBC库,这将成为: Statement statement = connection.c

我使用的是netbeans版本7.3.1,我只想在netbeans GUI中获取用户输入到多个文本字段中的数据,然后获取该数据并使用它在表中创建新行,在数据库中,我使用它创建新用户。我正在使用derby嵌入式

您应该使用(JDBC)将Java代码连接到数据库

SQL命令是:

INSERT INTO table_name
VALUES (value1,value2,value3,...);
使用Java和JDBC库,这将成为:

Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO table_name " + "VALUES (value1,value2,value3,...");
正如程序员所建议的,您还可以使用
PreparedStatement

String insertSQL = "INSERT INTO table_name (column1, column2) VALUES (?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);
preparedStatement.setString(1, "value1");
preparedStatement.setString(2, "value2");
preparedStatement.executeUpdate();

首选
PreparedStatement
,因为它可以消除潜在SQL注入攻击的风险。首先,您可以查看并了解您使用的GUI框架?