SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Java / SQLite Java: Inserting Data

SQLite Java: Inserting Data

Summary: in this tutorial, we will show you how to insert data into a table in an SQLite database using the Java JDBC.

To insert data into a table using the INSERT statement, you use the following steps:

  1. First, connect to the SQLite database.
  2. Next, prepare the INSERT statement. If you use parameters for the statement, use a question mark (?) for each parameter.
  3. Then, create an instance of the PreparedStatement from the Connection object.
  4. After that, set the corresponding values for each placeholder using the set method of the PreparedStatement object such as setInt(), setString(), etc.
  5. Finally, call the executeUpdate() method of the PreparedStatement object to execute the statement.

The following program inserts three rows into the table warehouses that we created in the creating table tutorial.

package net.sqlitetutorial; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; /** * * @author sqlitetutorial.net */ public class InsertApp { /** * Connect to the test.db database * * @return the Connection object */ private Connection connect() { // SQLite connection string String url = "jdbc:sqlite:C://sqlite/db/test.db"; Connection conn = null; try { conn = DriverManager.getConnection(url); } catch (SQLException e) { System.out.println(e.getMessage()); } return conn; } /** * Insert a new row into the warehouses table * * @param name * @param capacity */ public void insert(String name, double capacity) { String sql = "INSERT INTO warehouses(name,capacity) VALUES(?,?)"; try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setString(1, name); pstmt.setDouble(2, capacity); pstmt.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * @param args the command line arguments */ public static void main(String[] args) { InsertApp app = new InsertApp(); // insert three new rows app.insert("Raw Materials", 3000); app.insert("Semifinished Goods", 4000); app.insert("Finished Goods", 5000); } }

After running the program, you can check the warehouses table in the test.db database using the following SELECT statement:

SELECT id, name, capacity FROM warehouses;
SQlite Java Insert

In this tutorial, you have learned how to insert data into a table in the SQLite database from the Java program.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Java: Select Data
Next SQLite Java: Update Data

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All Rights Reserved.