java 生成sqlite3 DB文件

1.安裝sqlite等入門能夠查看:http://yuanzhifei89.iteye.com/blog/1123870java

或者紅薯大大的http://www.oschina.net/question/12_53183sql

2.須要導入sqlite-jdbc的jar包,下載地址:https://bitbucket.org/xerial/sqlite-jdbc/downloadstomcat

我使用的是sqlite-jdbc-3.8.11.jar,而後放在WEB-INF的lib下直接加入就好。app


import java.sql.*;

private void createRssDB(List<Byte> byteList,String firmId) throws SQLException, Exception {
		Connection conn=null;
		PreparedStatement prep=null;
		PreparedStatement rssInfoPrep=null;
		InputStream in = null;
		try{
			Class.forName("org.sqlite.JDBC");   
			String url=Thread.currentThread().getContextClassLoader().getResource("/").toString().substring(5);
		    conn = DriverManager.getConnection("jdbc:sqlite:"+url+"rss.db");
		    LOG.info("rss.db的路徑"+url+"rss.db");
		    Statement stat = conn.createStatement();   
		    stat.executeUpdate("drop table if exists rssType;");   
		    StringBuffer rssTableSql=new StringBuffer("create table rssType (");
		    rssTableSql.append("id INTEGER, name NTEXT,");
		    rssTableSql.append("typeId INTEGER,logoUrl NTEXT,");
		    rssTableSql.append("sort INTEGER);");
		    stat.executeUpdate(rssTableSql.toString());
		    LOG.info("建立rssTypeSql:"+rssTableSql.toString());
		    prep = conn.prepareStatement(   
		            "insert into rssType values (?, ?, ?, ?, ?);"); 
		    List<SubscribeManage> list=this.getRssType(firmId);
		    if(list.size()>0){
		        for (SubscribeManage subscribeManage : list) {
		        	prep.setInt(1,subscribeManage.getId());
		        	prep.setString(2,subscribeManage.getRssCategory());
		        	prep.setInt(3,subscribeManage.getId());
		        	prep.setString(4,subscribeManage.getRssImgUrl());
		        	prep.setInt(5,subscribeManage.getOrder());
		        	prep.addBatch();
				}
		    }
		    conn.setAutoCommit(false);   
		    prep.executeBatch();   
		    conn.setAutoCommit(true);
		    
		    Statement stats = conn.createStatement();   
		    stats.executeUpdate("drop table if exists rssInfo;");   
		    StringBuffer rssInfoSql=new StringBuffer("create table rssInfo (");
		    rssInfoSql.append("id INTEGER, name NTEXT,");
		    rssInfoSql.append("itr INTEGER,typeId INTEGER,");
		    rssInfoSql.append("snum INTEGER,url NTEXT,");
		    rssInfoSql.append("isSubs INTEGER,sort NTEXT,");
		    rssInfoSql.append("vurl NTEXT,wid INTEGER,");
		    rssInfoSql.append("subTime TIMESTAMP);");
		    stats.executeUpdate(rssInfoSql.toString());
		    
		    rssInfoPrep = conn.prepareStatement(   
		            "insert into rssInfo(id,name,itr,typeId,url,sort) values (?, ?, ?, ?, ?, ?);"); 
		    
		    List<SubscribeDTO> subList=subscribeService.getRssInfo(firmId);
		    if(subList.size()>0){
		        for (SubscribeDTO subscribeDTO : subList) {
		        	rssInfoPrep.setInt(1, subscribeDTO.getId());
		        	rssInfoPrep.setString(2, subscribeDTO.getSubName());
		        	rssInfoPrep.setString(3, subscribeDTO.getInfo());
		        	rssInfoPrep.setInt(4, subscribeDTO.getSubManageId());
		        	rssInfoPrep.setString(5, subscribeDTO.getRssImgUrl());
		        	rssInfoPrep.setInt(6, subscribeDTO.getOrder());
		        	rssInfoPrep.addBatch();
				}
		    }
		    conn.setAutoCommit(false);   
		    rssInfoPrep.executeBatch();   
		    conn.setAutoCommit(true);
		    
		    in=new FileInputStream(url+"rss.db");
			byte[] b = IOUtils.toByteArray(in);
			for (int i = 0; i < b.length; i++) {
				byteList.add(b[i]);
			}
		    LOG.info("rss.db的路徑"+url+"rss.db");
		}catch(Exception e){
			LOG.info("異常:"+e.getMessage());
			e.printStackTrace();
		}finally{
			if(prep!=null){
				prep.close();
			}
			if(rssInfoPrep!=null){
				rssInfoPrep.close();
			}
		    if(conn!=null){
		    	conn.close();
		    } 
		    if(in!=null){
		    	in.close();
		    }
		}
	}

會在項目中產生rss.db,F5刷新一下工程就會看到。this

後來我在糾結生成rss.db的生成路徑的時候發現若是這段代碼放在controller中是產生在項目中,若是放在service調用產生在tomcat路徑下,我就很疑惑,而後我就指定了路徑。url

linus下安裝sqlite3:.net

http://wenku.baidu.com/link?url=ceRgbTMawLQhnLJjehpaGHRSDCPUVUc8p7rlylVfzm_4K_OMJkCA3DH0xs2wEirIuJcxvDqzCe4NLUhB_EMr7v7gzwpjEsPCOfwKO1PID1u code

相關文章
相關標籤/搜索