日期:2014-05-16  浏览次数:20446 次

关于JDBC下使用SQLite DB问题总结(Windows下和Linux下使用)
关于JDBC下使用SQLite DB问题总结
       因为公司的服务器在Linux下,同时也需要在Windows下运行,所以这方面的工作必须做的仔细。以下几点收获可以给大伙分享,避免因为一些低级错误影响项目开发。
      在Windows下SQLite JDBC 路径必须是小写,并且路径中需要添加/
      在Linux下SQLite JBDC路径严格区分大小写,并且路径中不需要添加/
      所以在使用过程一定要注意:以下几行代码就是判断这个环境的:
public synchronized List getPetternList(String pattern) throws Exception{
		log.info("getPetternList DB="+pattern);
		String path = MLotteryContext.getInstance().get("pettern.dir");
		File pathDir = new File(path);
		if(!pathDir.exists())
			pathDir.mkdirs();
		log.info("pathDir = "+pathDir);
		String petternPath = path+"pattern.db";
		File newFile = new File(petternPath);
		log.info("==========start delete Old Pettern DB==========");
		deleteFile(newFile);
		log.info("==========End   delete Old Pettern DB==========");
		log.info("======Start =======Write Pettern DB===============");
		//------------------------
//		if (!newFile.exists()) {
//			newFile.createNewFile();
//		}
//		log.info("=============setExecutable===============");
//		newFile.setExecutable(true);
//		log.info("=============setReadable=================");
//		newFile.setReadable(true);
//		log.info("=============setWritable=================");
//		newFile.setWritable(true);
//		InputStream inStream = new FileInputStream(pattern);
//		FileOutputStream fs = new FileOutputStream(newFile);
//		byte[] buffer = new byte[1444];
//		int bytesum = 0;
//		int byteread = 0;
//		while ((byteread = inStream.read(buffer)) != -1) {
//			bytesum += byteread;
//			log.info("=============bytesum===="+bytesum);
//		//	System.out.println(bytesum);
//			fs.write(buffer, 0, byteread);
//		}
//		inStream.close();
//		log.info("======End  =======Write Pettern DB===============");
		
		File oldFile = new File(pattern);
		boolean bool = oldFile.renameTo(newFile);
		log.info("Current PetternDB:"+newFile.getPath()+" bool = " + bool);
		Class.forName("org.sqlite.JDBC");
		log.info("DB="+petternPath);
		
		log.info("====Start get Server System Type ==========");
		Properties props=System.getProperties(); //获得系统属性集
		String osName = props.getProperty("os.name"); //操作系统名称
		log.info("Current OS Type :"+osName);
		log.info("====End  get Server System Type ==========");
		
		osName = osName.toLowerCase();
		int index = 0;
		index = osName.indexOf("windows");
		String JDBC = "jdbc:sqlite:"+petternPath;
		if(index > -1){
			petternPath = petternPath.toLowerCase();
			JDBC="jdbc:sqlite:/"+petternPath;
		}
		log.info("path = "+petternPath);
		log.info("JDBC: "+JDBC);
		Connection conn = DriverManager.getConnection(JDBC);
		log.info("getPetternList conn="+conn.toString());
		Statement stat = conn.createStatement();
		ResultSet rs = stat.executeQuery("select * from pattern");
		List<String> list = null;
		if(rs != null){
			list = new ArrayList<String>();
		}
		while (rs.next()) {
			String pid = rs.getString("pattern_id");
			log.info("pattern_id = "+pid);
			list.add(pid);
		}
		rs.close();
		conn.close();
		deleteFile(newFile);
		return list;
}
只有这样做才能避免老是出现: