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