Sqlite-java Beginner

Source: Internet
Author: User
Tags sqlite

Come in. Prepare to use a embedded database, which is embedded, which facilitates local storage with the project. The current study is intended to be sqlite and H2.

Document:http://www.runoob.com/sqlite/sqlite-java.html

1. Connect to the database

To add a dependent JDBC:

<dependency>            <groupId>org.xerial</groupId>            <artifactid>sqlite-jdbc</ Artifactid>            <version>3.8.11.2</version></dependency>

  

Link code:

 PackageCom.test.database.sqlite;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;/*** Created by Miaorf on 2016/6/20.*/ Public classSQLITEJDBC { Public Static voidMain (string[] args) {Connection C=NULL; Try{class.forname ("Org.sqlite.JDBC"); C= Drivermanager.getconnection ("jdbc:sqlite:test.db"); } Catch(ClassNotFoundException e) {e.printstacktrace (); }Catch(SQLException e) {e.printstacktrace (); } System.out.println ("Open databse successfully"); }}

At the end of execution, a file called Test.db is generated at the root of the project, and this is our database.

2. Create a table

1  PackageCom.test.database.sqlite;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.SQLException;6 Importjava.sql.Statement;7 8 /**9 * Created by Miaorf on 2016/6/20.Ten  */ One  Public classSQLITEJDBC { A  -      Public Static voidMain (string[] args) { -  theConnection C =NULL; -Statement stmt =NULL; -         Try { -Class.forName ("Org.sqlite.JDBC"); +C= drivermanager.getconnection ("Jdbc:sqlite:test.db"); -SYSTEM.OUT.PRINTLN ("opened database successfully"); +  Astmt =c.createstatement (); atString sql = "CREATE TABLE Company" + -"(ID INT PRIMARY KEY not NULL," + -"NAME TEXT not NULL," + -"Age INT is not NULL," + -"ADDRESS CHAR (50)," + -"SALARY REAL"); in stmt.executeupdate (SQL); - stmt.close (); to c.close (); +  -SYSTEM.OUT.PRINTLN ("CREATE table successfully"); the  *}Catch(ClassNotFoundException e) { $ e.printstacktrace ();Panax Notoginseng}Catch(SQLException e) { - e.printstacktrace (); the         } +  A  the     } +  -  $}

Sqlite-java Beginner

Related Article

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.