首页>代码>Java zookeeper开发实例>/access/src/main/java/com/hpgary/access/Main.java
package com.hpgary.access;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.jdbc.datasource.DataSourceUtils;

public class Main {

	public static void update(String sql) {
		DataSource data = getDataSource();
		Connection conn = null;
		try {
			conn = data.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			try {
				DataSourceUtils.doCloseConnection(conn, dataSource);
			} catch (Exception e) {
			}
		}
	}

	protected static BasicDataSource dataSource = null;

	public static DataSource getDataSource() {
		synchronized (Thread.class) {
			if (null == dataSource) {
				dataSource = new BasicDataSource();
				dataSource.setUrl("jdbc:sqlite:hp.db");
				dataSource.setDriverClassName("org.sqlite.JDBC");
			}
		}
		return dataSource;
	}

	public static void main(String[] args) throws Exception {
		update("drop table if exists COMPANY");
		update("CREATE TABLE COMPANY (ID INT,cname VARCHAR(40))");

		for (int x = 0; x < 300; x++) {
			update("insert into COMPANY(id , cname) values(" + x + " ,'xx" + x + "')");
		}

		PreparedStatement ps = getDataSource().getConnection().prepareStatement("select * from COMPANY");
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			System.out.println(rs.getString("id") + "--" + rs.getString("cname"));
		}
	}
}
最近下载更多
cgp0219  LV6 2020年10月27日
crazyBear2020  LV3 2020年1月15日
gmhaizn  LV10 2018年3月30日
liangzai123  LV18 2018年3月21日
kildee  LV11 2017年7月6日
最近浏览更多
流水本无情  LV9 2024年3月13日
2602275348  LV12 2022年3月31日
yang12  LV1 2021年6月15日
sunbing 2021年3月29日
暂无贡献等级
1529860026  LV24 2020年11月26日
liyiming  LV4 2020年8月6日
943054597 2020年8月6日
暂无贡献等级
guaixia163  LV13 2020年7月13日
lcjssm  LV5 2020年7月5日
夜上清元  LV8 2020年6月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友