Introduction of sqlite database and illustration of java's operation on sqlite

  • 2021-06-29 10:58:36
  • OfStack

What is sqlite?

1. A Lightweight Database 2. Relational Database 3. Low resource consumption, hundreds of K memory, suitable for embedded devices 4. Support windows, linux, unix 5. Can be combined with java, php, c#, python, etc. 6. Processing speed is faster than mysql 7. No configuration, no installation, no management required 8. A complete SQLite database is a cross-platform disk file stored in a single 1. Simply put, a database is a single 1 file

Why use it?

The mysql database that was used by previous web project 1 was chosen because the current project needs to make a desktop application that can be reused in different places. We cannot guarantee that all application environments have the mysql environment installed, so we choose sqlite as an install-free, single-file database to deploy this application to other environments.Put the database file in the project 1 file upload in the past OK!

java Operation sqlite

Download an jar package from sqlite-jdbc and place it in the project;

Write a test class to express how to use sqlite:


public class TestSQLite{
   public static void main(String[] args){
     try{
     // Connect SQLite Of JDBC
     Class.forName("org.sqlite.JDBC");   
     // establish 1 Database names zieckey.db Connection, created in the current directory if it does not exist 
     Connection conn = DriverManager.getConnection("jdbc:sqlite:zieckey.db");  
     Statement stat = conn.createStatement();
     stat.executeUpdate( "create table tbl1(name varchar(20), salary int);" );// Establish 1 Table, two columns 
     stat.executeUpdate( "insert into tbl1 values('ZhangSan',8000);" ); // insert data 
     stat.executeUpdate( "insert into tbl1 values('LiSi',7800);" );
     stat.executeUpdate( "insert into tbl1 values('WangWu',5800);" );
     stat.executeUpdate( "insert into tbl1 values('ZhaoLiu',9100);" ); 
     ResultSet rs = stat.executeQuery("select * from tbl1;"); // Query Data  
     while (rs.next()) { // Print out the queried data 
       System.out.print("name = " + rs.getString("name") + " "); // Column Properties 1
       System.out.println("salary = " + rs.getString("salary")); // Column Properties 2
     }
     rs.close();
     conn.close(); // End connection to database  
     }
     catch( Exception e )
     {
     e.printStackTrace ( );
     }
   }
 }

Similar to the java operation of other databases, it mainly takes advantage of its install-free and single-file nature.

MISSION SUCCESS

summary


Related articles: