SQLite getting started study Note 2

Source: Internet
Author: User

I have learned how to create tables and add, delete, modify, and query tables on the SQLite console interface. The following describes how to use Java to operate the SQLite database.

1. Use eclipse in the development environment, create a Java project mysqlitetest, and create a class testsqlite. Java

2. Download The jdbc of the SQLite database. Here is the URL of a Chinese site:
Http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz

3. decompress the downloaded package and obtain the jar package sqlitejdbc-v033-nested.jar. export it to the additional package path of mysqlitetest just created.

4. Write the following code:

import java.sql. *;
import org.sqlite.JDBC;

public class TestSQLite
{
    public static void main (String [] args)
    {
        try
        {
         // JDBC for SQLite
         Class.forName ("org.sqlite.JDBC");
        // Establish a connection to the database name test.db.
         Connection conn = DriverManager.getConnection ("jdbc: sqlite: / d: /test.db");
         
         Statement stat = conn.createStatement ();
         stat.executeUpdate ("create table user (name varchar (20), salary int);"); // Create the user table
         stat.executeUpdate ("insert into user values ('LiSi', 7800);"); // Insert data
           stat.executeUpdate ("insert into user values ('WangWu', 5800);");
         stat.executeUpdate ("insert into user values ('Test', 9100);");
         ResultSet rs = stat.executeQuery ("select * from user;"); // Query data

         while (rs.next ()) {// Print out the query data

             System.out.print ("name =" + rs.getString ("name") + ""); // Column property one

             System.out.println ("salary =" + rs.getString ("salary")); // Column property two

         }
         rs.close ();
         conn.close (); // End the database connection

        }
        catch (Exception e)
        {
         e.printStackTrace ();
        }
    }
} 

The running result is as follows:

Name = Lisi salary = 7800
Name = wangwu salary = 5800
Name = zhaoliu salary = 9100

This example program is available in Baidu encyclopedia. We can see that two statements are used to connect to the database. create a table in the SQL database and insert the data for query. This is the same as using JDBC to operate the access/sqlserver database, but with a different driver when connecting to the database

The following describes how to apply it to a web program for user registration and login. Registration is actually a data table insertion Operation, and login is a table query operation.

 

References:

1. http://baike.baidu.com/view/19310.htm

2. http://secyaher.blog.163.com/blog/static/38955772010102772217537/

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.