Java Connection to SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database

Download Java package: Sqlite-jdbc-3.7.2.jar, put in the Java project directory Lib

The following code example:

Import java.sql.*; import Org.sqlite.JDBC;/** This is a very simple SQLite Java program, * The program to create a database, create a table, and then insert data, * Finally read out the data display*/ Public classSqlitetest { Public Static voidMain (string[] args) {Try        {         //connect to SQLite's JDBCClass.forName ("Org.sqlite.JDBC"); //Create a connection to the database name Lincj.db if it does not exist in the current directoryConnection conn = Drivermanager.getconnection ("jdbc:sqlite:lincj.db"); //Connection conn = Drivermanager.getconnection ("Jdbc:sqlite:path (path)/lincj.db");Statement stat =conn.createstatement (); Stat.executeupdate ("CREATE TABLE table1 (name varchar (UP), age int);");//Create a table, two columnsStat.executeupdate ("INSERT INTO table1 values (' AA ', +);");//Inserting DataStat.executeupdate ("INSERT INTO table1 values (' BB ', +);" ); Stat.executeupdate ("INSERT INTO table1 values (' cc ', +);" ); ResultSet RS= Stat.executequery ("select * FROM table1;");//Querying Data          while(Rs.next ()) {//Print the data you have queriedSystem. out. Print ("name ="+ rs.getstring ("name") +" ");//Column Properties OneSystem. out. println ("Age ="+ rs.getstring (" Age"));//Column Properties Two} rs.close (); Conn.close (); //to end a connection to a database        }        Catch(Exception e) {e.printstacktrace (); }    }}

Java Connection to SQLite database

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.