[Sqlite]--)Java使用jdbc连接Sqlite数据库进行各种数据操作的详
前言: SQLite是遵守ACID 的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。 一,准备sqlite数据源 由于sqlite是内存数据库,还会有一个数据文件,本质上是去访问一个文件,所以可以把linux下的sqlite的库文件copy
前言:
SQLite是遵守ACID 的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。
一,准备sqlite数据源
由于sqlite是内存数据库,还会有一个数据文件,本质上是去访问一个文件,所以可以把linux下的sqlite的库文件copy出来到自己笔记本本地,然后在调用java程序访问操作sqlite库。
二,开始准备Eclipse环境
加载jdbc的jar包,jdbc的jar包 sqlitejdbc-v033-nested.jar,下载地址为:http://pan.baidu.com/s/1hqj7tT6
三,编写java代码连接sqlite数据库操作
(1),创建java代码测试类
•package foo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @author Tim
*/
public class JavaSqlite {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// 0 连接SQLite的JDBC
String sql=\"jdbc:sqlite://e:/tim.db\";
Class.forName(\"org.sqlite.JDBC\");
// 1 建立一个数据库名zieckey.db的连接,如果不存在就在当前目录下创建之
Connection conn = DriverManager.getConnection(sql);
Statement stat = conn.createStatement();
// 2 创建一个表tbl1,录入数据
stat.executeUpdate(\"drop table if exists tbl1;\");
stat.executeUpdate(\"create table if not exists tbl1(name varchar(20), salary int);\");// 创建一个表,两列
stat.executeUpdate(\"insert into tbl1 values(\'ZhangSan\',8000);\"); // 插入数据
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;\"); // 查询数据
System.out.println(\"创建表结构录入数据操作演示:\");
while (rs.next()) { // 将查询到的数据打印出来
System.out.print(\"name = \" + rs.getString(\"name\") + \", \"); // 列属性一
System.out.println(\"salary = \" + rs.getString(\"salary\")); // 列属性二
}
rs.close();
// 3 修改表结构,添加字段 address varchar(20) default \'changsha\';
stat.executeUpdate(\"alter table tbl1 add column address varchar(20) not null default \'changsha\'; \");// 创建一个表,两列
stat.executeUpdate(\"insert into tbl1 values(\'HongQi\',9000,\'tianjing\');\"); // 插入数据
stat.executeUpdate(\"insert into tbl1(name,salary) values(\'HongQi\',9000);\"); // 插入数据
rs = stat.executeQuery(\"select * from tbl1;\"); // 查询数据
System.out.println(\"表结构变更操作演示:\");
while (rs.next()) { // 将查询到的数据打印出来
System.out.print(\"name = \" + rs.getString(\"name\") + \", \"); // 列属性一
System.out.print(\"name = \" + rs.getString(\"name\") + \", \"); // 列属性二
System.out.println(\"address = \" + rs.getString(\"address\")); // 列属性三
}
rs.close();
conn.close(); // 结束数据库的连接
} catch (Exception e) {
e.printStackTrace();
}
}
}
四,调试运行:
(1),第一次调试报错如下:
五,运行结果,代码类里面右键选择Run As,再选择Java Application,执行结果如下所示:
创建表结构录入数据操作演示:
操作界面如下所示:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.
