[Sqlite] --) details about how to use jdbc to connect to the Sqlite database for various data operations

Source: Internet
Author: User
SQLite is an ACID-compliant relational database management system, which is contained in a relatively small C library. It is a public project established by D. Richard IPP. I. Prepare the sqlite data source. Since sqlite is a memory database, there will also be a data file, which is essentially to access a file, so you can copy the sqlite library file in linux

SQLite is an ACID-compliant relational database management system, which is contained in a relatively small C library. It is a public project established by D. Richard IPP. I. Prepare the sqlite data source. Since sqlite is a memory database, there will also be a data file, which is essentially to access a file, so you can copy the sqlite library file in linux

Preface:

SQLite is a ACID-compliant relational database management system, which is contained in a relatively small C library. It is a public project established by D. Richard IPP.

1. Prepare the sqlite Data Source

Since sqlite is a memory database, there will also be a data file, essentially accessing a file, so you can copy the sqlite library file in linux to your laptop, then, access the sqlite library by calling the java program.

2. Start preparing the Eclipse Environment

Load jdbc jar package, jdbc jar package sqlitejdbc-v033-nested.jar, for: http://pan.baidu.com/s/1hqj7tT6

3. Write java code to connect to the sqlite Database

(1) create a java code test class
• Package foo;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. Statement;
/**
* @ Author Tim
*/
Public class extends qlite {
Public static void main (String [] args ){
// TODO Auto-generated method stub
Try {
// 0 connect to the JDBC of SQLite
String SQL = \ "jdbc: sqlite: // e:/tim. db \";
Class. forName (\ "org. sqlite. JDBC \");
// 1 create a connection with the database name zieckey. db. If it does not exist, create it in the current directory
Connection conn = DriverManager. getConnection (SQL );
Statement stat = conn. createStatement ();
// 2 create a table tbl1 and input data
Stat.exe cuteUpdate (\ "drop table if exists tbl1 ;\");
Stat.exe cuteUpdate (\ "create table if not exists tbl1 (name varchar (20), salary int); \"); // create a table with two columns
Stat.exe cuteUpdate (\ "insert into tbl1 values (\ 'hangsan \ ', 8000); \"); // insert data
Stat.exe cuteUpdate (\ "insert into tbl1 values (\ 'lisi \ ', 7800 );\");
Stat.exe cuteUpdate (\ "insert into tbl1 values (\ 'wangwu \ ', 5800 );\");
Stat.exe cuteUpdate (\ "insert into tbl1 values (\ 'zhaoliu \ ', 9100 );\");
ResultSet rs = stat.exe cuteQuery (\ "select * from tbl1; \"); // query data
System. out. println (\ "create table Structure Input data operation Demo :\");
While (rs. next () {// print the queried data
System. out. print (\ "name = \" + rs. getString (\ "name \") + \ ", \"); // column attribute 1
System. out. println (\ "salary = \" + rs. getString (\ "salary \"); // column attribute 2
}
Rs. close ();
// 3 modify the table structure and add the field address varchar (20) default \ 'changsha \';
Stat.exe cuteUpdate (\ "alter table tbl1 add column address varchar (20) not null default \ 'changsha \ '; \"); // create a table with two columns
Stat.exe cuteUpdate (\ "insert into tbl1 values (\ 'hongqi \ ', 9000, \ 'tianjing \'); \"); // insert data
Stat.exe cuteUpdate (\ "insert into tbl1 (name, salary) values (\ 'hongqi \ ', 9000); \"); // insert data
Rs = stat.exe cuteQuery (\ "select * from tbl1; \"); // query data
System. out. println (\ "table structure change operation Demo :\");
While (rs. next () {// print the queried data
System. out. print (\ "name = \" + rs. getString (\ "name \") + \ ", \"); // column attribute 1
System. out. print (\ "name = \" + rs. getString (\ "name \") + \ ", \"); // column attribute 2
System. out. println (\ "address = \" + rs. getString (\ "address \"); // column attribute 3
}
Rs. close ();
Conn. close (); // end the database connection
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

4. debug and run:

(1) The first debugging reports the following error:

5. Run the result. Right-click the code class and choose Run As and then select Java Application. The execution result is As follows:

Create Table Structure Input data operation Demonstration:

The operation interface is as follows:

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.