我是想做一个类似这种推广的方式,在网上看到很多网页都有这种,就想自己用java来实现这个模块。
这个和sqlite有什么关系,这是js实现的图片轮播
你是做android-java,是sunjdk-java
下面的android例子,如果是普通java,找相关jdbc驱动,换换getConnection方式就可以了。
不过建议你,如果是写android-java访问sqlite,建议不用SQL事务,因为单文件死锁几率比较大。
package com.chobits.db;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDiskIOException;
import android.util.Log;
public class DBFactory {
private SQLiteDatabase db = null;
private String dbFilePath = null;
private boolean autoCommit = true;
public DBFactory(String dbFilePath) {
this.dbFilePath = dbFilePath;
}
public void openTransaction() throws Exception{
long timeout = 30000;
long start = System.currentTimeMillis();
SQLiteDiskIOException databaseLockedException = null;
while (System.currentTimeMillis() - start < timeout) {
try {
db = SQLiteDatabase.openDatabase(dbFilePath, null, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.OPEN_READWRITE);
databaseLockedException = null;
break;
} catch (SQLiteDiskIOException e) {
if (databaseLockedException == null) {
databaseLockedException = e;
}
System.out.println("Exception opening SQLiteDatabase: " + e.getClass() + " " + e.getMessage());
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
}
}
}
if (db == null) {
throw new SQLException("Timeout opening database");
}
}
public void commitTransaction() throws Exception{
if (autoCommit){
throw new SQLException("database in auto-commit mode");
}
db.setTransactionSuccessful();
db.endTransaction();
db.beginTransaction();
}
public void rollbackTransaction() throws Exception{
if (autoCommit){
throw new SQLException("database in auto-commit mode");
}
db.endTransaction();
db.beginTransaction();
}
public void close(){
if(db != null){
db.close();
}
db = null;
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
if (this.autoCommit == autoCommit)
return;
this.autoCommit = autoCommit;
if (autoCommit) {
db.setTransactionSuccessful();
db.endTransaction();
} else {
db.beginTransaction();
}
}
public boolean isClosed(){
return db == null || !db.isOpen();
}
private String trackValue(Object object){
if(object == null || object.toString().trim().length()==0 || object.toString().trim().equals("null") || object.toString().trim().equals("NULL")){
return "";
}else{
return object.toString();
}
}
protected void finalize() throws Throwable {
if (db != null)
db.close();
db = null;
super.finalize();
}
public void executeUpdate(String sql) throws Exception{
Log.d("DBFactory", sql);
db.execSQL(sql);
}
public void executeUpdate(String sql, Object[] args) throws Exception{
Log.d("DBFactory", sql);
for(int i=0;args!=null && i<args.length;i++){
Log.d("DBFactory", String.valueOf(args[i]));
}
db.execSQL(sql, args);
}
public List<Map<String,Object>> queryList(String sql) throws Exception{
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Cursor cursor = null;
try{
Log.d("DBFactory", sql);
cursor = db.rawQuery(sql, null);
if(cursor.getCount()>0){
int columnCount = cursor.getColumnCount();
cursor.moveToFirst();
do{
Map<String,Object> map = new HashMap<String,Object>();
for(int i=0;i<columnCount;i++){
String columnName = cursor.getColumnName(i);
map.put(columnName.toUpperCase(), this.trackValue(cursor.getString(i)));
}
list.add(map);
}while(cursor.moveToNext());
}
if(cursor!=null){
cursor.close();
cursor = null;
}
}catch(Exception e){
if(cursor!=null){
cursor.close();
cursor = null;
}
throw e;
}
return list;
}
public List<Map<String,Object>> queryList(String sql, String[] args) throws Exception{
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Cursor cursor = null;
try{
Log.d("DBFactory", sql);
for(int i=0;args!=null && i<args.length;i++){
Log.d("DBFactory", String.valueOf(args[i]));
}
cursor = db.rawQuery(sql, args);
if(cursor.getCount()>0){
int columnCount = cursor.getColumnCount();
cursor.moveToFirst();
do{
Map<String,Object> map = new HashMap<String,Object>();
for(int i=0;i<columnCount;i++){
String columnName = cursor.getColumnName(i);
map.put(columnName.toUpperCase(), this.trackValue(cursor.getString(i)));
}
list.add(map);
}while(cursor.moveToNext());
}
if(cursor!=null){
cursor.close();
cursor = null;
}
}catch(Exception e){
if(cursor!=null){
cursor.close();
cursor = null;
}
throw e;
}
return list;
}
public Map<String,Object> queryMap(String sql) throws Exception{
Map<String,Object> map = null;
Cursor cursor = null;
try{
Log.d("DBFactory", sql);
cursor = db.rawQuery(sql, null);
int columnCount = cursor.getColumnCount();
if(cursor.moveToFirst()){
map = new HashMap<String,Object>();
for(int i=0;i<columnCount;i++){
String columnName = cursor.getColumnName(i);
map.put(columnName.toUpperCase(), this.trackValue(cursor.getString(i)));
}
}
if(cursor!=null){
cursor.close();
cursor = null;
}
}catch(Exception e){
if(cursor!=null){
cursor.close();
cursor = null;
}
throw e;
}
return map;
}
public long getCount(String table, String indexName, String solution) throws Exception{
long count = 0;
Cursor cursor = null;
try{
String sql = "";
if(solution == null || solution.equals("")){
if(indexName==null || indexName.equals("")){
sql = "select count(*) as TTT_NO from "+table;
}else{
sql = "select count("+indexName+") as TTT_NO from "+table;
}
}else{
if(indexName==null || indexName.equals("")){
sql = "select count(*) as TTT_NO from "+table+" where "+solution;
}else{
sql = "select count("+indexName+") as TTT_NO from "+table+" where "+solution;
}
}
Log.i("DBFactory", sql);
cursor = db.rawQuery(sql, null);
if(cursor.moveToFirst()){
count = Long.parseLong(cursor.getString(0));
}
if(cursor!=null){
cursor.close();
cursor = null;
}
}catch(Exception e){
if(cursor!=null){
cursor.close();
cursor = null;
}
throw e;
}
return count;
}
public long getMaxValue(String table, String indexName) throws Exception{
long count = 0;
Cursor cursor = null;
try{
String sql = "";
if(indexName==null || indexName.equals("")){
throw new Exception("indexName must not be null or empty");
}else{
sql = "select max("+indexName+") as TTT_NO from "+table;
}
cursor = db.rawQuery(sql, null);
if(cursor.moveToFirst()){
count = Long.parseLong(cursor.getString(0));
}
if(cursor!=null){
cursor.close();
cursor = null;
}
}catch(Exception e){
if(cursor!=null){
cursor.close();
cursor = null;
}
throw e;
}
return count;
}
public boolean isAutoCommit() {
return autoCommit;
}
}