Inserting a Row into a Database Table Using a Prepared Statement : Table « Database SQL JDBC « Java

Inserting a Row into a Database Table Using a Prepared Statement


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";

    String serverName = "";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    String sql = "INSERT INTO my_table (col_string) VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    // Insert 10 rows
    for (int i = 0; i < 10; i++) {
      // Set the value
      pstmt.setString(1, "row " + i);

      // Insert the row



Related examples in the same category

1.Table exist?
2.List tables in a database
3.Listing All Table Names in a Database
4.Get all table schemas
5.Get all table catalogs
6.Another Method To Check Table Existance
7.Get Catalog Name From ResultSet Metadata
8.Get Available Table Name In A Database
9.Get Table Or View Name From A Database
10.Database metadata: data type
11.Deleting a Table from Database
12.Retrieving Tables from a Database
13.List the Tables in an SQL database
14.Copy One Database Table to Another
15.Create table with primary key
16.Create table with foreign key
17.Create data table 3
18.Create supplier table
19.Create data table: reference
20.Create data table 2
21.Creating a Database Table called my_table with one column, col_string, which holds strings.
22.Deleting a Database Table called my_table from a database.
23.Getting Rows from a Database Table
24.Getting the Number of Rows in a Database Table
25.Inserting a Row into a Database Table
26.Updating a Row in a Database Table
27.Deleting a Row from a Database Table
28.Deleting All Rows from a Database Table
29.Create data type and data table
30.Table and column existance