Use sqlite3 to develop Java applications

Source: Internet
Author: User

JDBC

Here is the URL of a Chinese site:

Http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz

Decompress the downloaded package and get the jar package sqlitejdbc-v033-nested.jar to % java_home %/lib
,
And add it to the classpath system environment variable. My classpath system environment variable is:
.; % Java_home %/lib/sqlitejdbc-v033-nested.jar reference this driver in your code:
Class. forname ("org. SQLite. JDBC ");
Connection
Conn =
Drivermanager. getconnection ("JDBC: SQLite: FILENAME"); // filename is your SQLite data name

//... Use
The database...

Conn. Close ();

The example program is as follows:

import
java
.
sql
.
*
;

import
org
.
sqlite.
JDBC;

public
class
TestSQLite
{

    public
static
void
main(
String
[
]
args)

    {

        try

        {

// Connect to JDBC of SQLite


Class
.
Forname
(
"Org. SQLite. JDBC"
)
;

         

// Create a connection with the database name zieckey. DB. If it does not exist, create it in the current directory


Connection
Conn =
Drivermanager
.
Getconnection
(
"JDBC: SQLite: zieckey. DB"
)
;

         

         Statement
stat =
conn.
createStatement
(
)
;

         

Stat.
Executeupdate
(
"Create table tbl1 (name
Varchar (20), salary INT );"
)
;
// Create a table with two columns


Stat.
Executeupdate
(
"Insert into tbl1
Values ('hangsan ', 8000 );"
)
;
// Insert data


Stat.
Executeupdate
(
"Insert into tbl1
Values ('lisi', 7800 );"
)
;

Stat.
Executeupdate
(
"Insert into tbl1
Values ('wangw', 5800 );"
)
;

Stat.
Executeupdate
(
"Insert into tbl1
Values ('zoliu', 9100 );"
)
;

 
Resultset
Rs =
Stat.
Executequery
(
"Select * From tbl1 ;"
)
;
// Query data


While
(
Rs.
Next
(
)
)
{
// Print the queried data


System
.
Out.
Print
(
"Name
="
+
Rs.
Getstring
(
"Name"
)
+
""
)
;
// Column attribute 1


System
.
Out.
Println
(
"Salary
="
+
Rs.
Getstring
(
"Salary"
)
)
;
// Column attribute 2


}

         rs.
close
(
)
;

Conn.
Close
(
)
;
// End the database connection


}

        catch
(
Exception
e
)

        {

         e.
printStackTrace
(
)
;

        }

    }

}

Compile and run:

E:/coding/Java/test> javac testsqlite. Java

E:/coding/Java/test> JAVA testsqlite
Name =
Zhangsan
Salary= 8000
Name =
Lisi
Salary= 7800
Name =
Wangwu
Salary= 5800
Name =
Zhaoliu
Salary = 9100 summary: This article introduces the application of embedded database SQLite in Java,
This section describes how to operate databases in Java by creating tables, inserting data, and querying tables.

Sqlite3 command operations
========================================================== ========================>>

Database and table creation, record addition, query, modification, and deletion

F:/> sqlite3 database. DB
SQLite> Create Table admin (username text, age
Integer );
SQLite> insert into admin values ('kuang ', 25 );
SQLite> select * from admin;
SQLite> Update admin set username = 'kk ', age = 24 where
Username = 'kuang 'and age = 25;
SQLite> Delete from Admin where username = 'kk ';

Note: Each SQL statement must end!

2. SQLite system commands

. Bail
On | off
Stop after hitting
Error. Default off
. Databases
List names and files of attached databases (view the database currently on)
. Dump? Table?
... Dump
The database in an SQL text format (output table structure in SQL format)
. Echo
On | off
TURN Command echo on or off
. Exit exit
This program (exit Program)
. Explain
On | off turn
Output Mode suitable for explain on or off.
. Header (s)
On | off turn
Display of headers on or off
. Help show
This message (displays help information)
. Import File
Table
Import data from file
Table (import the data in the file to the table, and use the value of separator as the separator for each field)
. Indices
Table
Show names of all indices on table
. Load file
? Entry?
Load an extension Library
. Mode
? Table?
Set output mode where mode is one of :( output format)

CSV comma-separated
Values (each field is output with a comma as the separator)

Column left-aligned
Columns. (See
. Width) (display fields with the width set in. width)

Html
HTML <Table> code (HTML table format output)

Insert SQL insert statements
For Table (output in the form of an insert SQL statement)

Line
One value per line (field = value)

List
Values delimited by. Separator string (each field is output using the value of separator as the separator)

Tabs
Tab-separated values

TCL
List Elements
. Nullvalue
String print
String in place of null values
. Output
Filename
Send output to filename (set to output the query to the file, and save the subsequent output to the file)
. Output
Stdout
Send output to the screen (set to output the query result to the screen, default)
. Prompt main continue replace
The standard prompts (modify the prompt)
. Quit exit
This program (Exit)
. Read
Filename
Execute SQL in filename (SQL statement in the execution file)
. Schema
? Table? Show
The create statements (output table structure in SQL format)
. Separator
String change
Separator Used by output mode and. Import (modify separator)
. Show show
The current values for various settings (display configuration information)
. Tables
? Pattern? List
Names of tables matching a like pattern (see how many tables are created)
. Timeout
MS try
Opening locked tables for MS milliseconds (timeout, in milliseconds)
. Width num
...
Set column widths for "column" mode (set column width)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.