java sqlite_Java SQLite教程

java sqlite

In this tutorial you will learn about Java SQLite.

在本教程中,您将学习Java SQLite。

SQLite is lightweight, zero configuration, serverless SQL database library. In this tutorial I will teach you how to use SQLite database with Java.

SQLite是轻量级,零配置,无服务器SQL数据库库。 在本教程中,我将教您如何在Java中使用SQLite数据库。

Note: I have made this tutorial using Eclipse. Because it is easier to import library in an IDE.

注意:我已经使用Eclipse制作了本教程。 因为在IDE中更容易导入库。

First off all you need to download the SQLite JDBC library or jar. Download it from below link.

首先,您需要下载SQLite JDBC库或jar。 从下面的链接下载。

Download: http://www.java2s.com/Code/Jar/s/Downloadsqlitejdbc372jar.htm

下载: http : //www.java2s.com/Code/Jar/s/Downloadsqlitejdbc372jar.htm

Now just import it into your project. If you don’t know how to import library in Eclipse then follow below link.

现在,将其导入到您的项目中。 如果您不知道如何在Eclipse中导入库,请点击以下链接。

http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse

http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse

Java SQLite Tutorial

Java SQLite教程 (Java SQLite Tutorial)

与数据库连接 (Connection with Database)

Below example shows how you can connect Java with SQLite database. Here demo.db is the database name, you can change it according to you. If the database doesn’t exists then it will be created.

下面的示例显示如何将Java与SQLite数据库连接。 这里demo.db是数据库名称,您可以根据自己的需要进行更改。 如果数据库不存在,则将创建它。

package com;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
public class JavaSQLiteExample {
 
	public static void main(String args[]){
		try{
			
			//establish connection with database
			Class.forName("org.sqlite.JDBC");
			Connection con=DriverManager.getConnection("jdbc:sqlite:demo.db");
 
			if(con!=null){
				System.out.println("Connection established");
			}
			con.close();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Some error occured");
		}
	}	
}

On successful connection the program show output “Connection established” otherwise “Some error occurred”.

成功连接后,程序将显示输出“连接已建立”,否则显示“发生某些错误”。

Java SQLite示例 (Java SQLite Example)

The below program first establish connection with SQLite database, create a table, insert some record in it and then finally display the records. This shows that how you can work with SQLite using Java.

下面的程序首先与SQLite数据库建立连接,创建一个表,在其中插入一些记录,然后最后显示记录。 这说明了如何使用Java使用SQLite。

package com;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class JavaSQLiteExample {
 
	public static void main(String args[]){
		try{
			
			//establish connection with database
			Class.forName("org.sqlite.JDBC");
			Connection con=DriverManager.getConnection("jdbc:sqlite:demo.db");
			
			Statement st=con.createStatement();
			
			//create table
			System.out.println("Create table:");
			st.executeUpdate("drop table record");
			st.executeUpdate("create table record (name text,age int)");
			
			//insert some records 
			System.out.println("Insert some records:");
			st.executeUpdate("insert into record values('neeraj',21)");
			st.executeUpdate("insert into record values('mayank',22)");
			st.executeUpdate("insert into record values('sumit',22)");
 
			//reading records
			System.out.println("Reading records:");
			ResultSet rs=st.executeQuery("select * from record");
			
			while(rs.next()){
				System.out.println(rs.getString("name")+" "+rs.getString("age"));
			}
			
			rs.close();
			st.close();
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

Output

输出量

Java SQLite Tutorial Output

So this was the simple Java SQLite tutorial. You can comment below if you are facing any problem.

这就是简单的Java SQLite教程。 如果您遇到任何问题,可以在下面发表评论。

翻译自: https://www.thecrazyprogrammer.com/2016/04/java-sqlite-tutorial.html

java sqlite

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值