数据库连接

本文介绍了一个使用 Java 进行 SQLite 数据库操作的简单示例,包括连接数据库、执行增删改查等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package lianxi2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTool {
	/**
	 *  做连接的操作
	 */
	public static Statement  getConnection(String path){
		Statement statement = null;
		// 1 添加驱动
				try {
					Class.forName("org.sqlite.JDBC");
					//2 建立连接
					Connection conn = DriverManager.getConnection("jdbc:sqlite:/"+path);
					// 磁盘用小写
					// 3 添加执行sql语句的对象
					statement = conn.createStatement();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return statement;
	}
}


package lianxi2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlUtil {

	/**
	 * 查
	 */
	/**
	 * 
	 * @param table
	 *            表名
	 * @param whereCause
	 *            传递where中条件列名
	 * @param where
	 *            条件中跟列名对应的值
	 */
	public static void query(String table, String whereCause, String where,
			Statement statement) {

		String sql = "select * from " + table + " where " + whereCause + "="
				+ where;
		try {
			ResultSet set = statement.executeQuery(sql);
			// 循环打印结果
			while (set.next()) {
				System.out.println(set.getString("_id") + "|"
						+ set.getString(2) + "|" + set.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void query(String sql, Statement statement) {

		try {
			ResultSet set = statement.executeQuery(sql);
			// 循环打印结果
			while (set.next()) {
				System.out.println(set.getString("_id") + "|"
						+ set.getString(2) + "|" + set.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 *增
	 *insert
	 * @throws SQLException 
	 */
	
	public static int insert(String sql,Statement statement) throws SQLException{
		return statement.executeUpdate(sql);
	}
	
	/**
	 * 删
	 * @throws SQLException 
	 */
	
	public static int delete(String sql,Statement statement) throws SQLException{
		return statement.executeUpdate(sql);
	}
	/**
	 * 改
	 * @throws SQLException 
	 */
	public static int  update(String sql,Statement statement) throws SQLException{
		return statement.executeUpdate(sql);
	}
}

package lianxi2;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {

	public static void main(String[] args) throws ClassNotFoundException, SQLException, UnsupportedEncodingException {
		
		Statement statement = JDBCTool.getConnection("d:/demo.db;");
		
		// 插入数据    
		// sql语句
		String sql2 = "insert into users(name,age) values('上方宝剑',1000)";
//		
//		//如果其为更新计数或者不存在任何结果,则返回 false 
//		boolean b = statement.execute(sql2);
//		if(b){
//			System.out.println("插入成功");
//		}else {
//			System.out.println("真失败");
//		}
		System.out.println("插入成功条数"+SqlUtil.insert(sql2, statement));
		
		// 删除操作
		String sql3 = "delete from users where name='上方宝剑'";
//		int a = statement.executeUpdate(sql3);
		
		System.out.println("删除的条数"+SqlUtil.delete(sql3, statement));
		
		// 修改
		String sql4 = "update users set name = '李四'  where _id = 3";
		
		System.out.println("修改的条数"+SqlUtil.update(sql4, statement));
		
		// 查询
		SqlUtil.query("select * from users", statement);
		
		
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值