java - Class for MySQL and SQLite -


i'm programming bukkit plugin, server "minecraft" game. don't know how write sql, syntax.

i looking java class access mysql , sqlite. plugin use 1 database @ time, set in configuration file, mysql or sqlite.

i've found this:

import java.io.*; import java.sql.*; import java.util.logging.*; import org.bukkit.plugin.*;  public class db {     private final plugin plugin;     private final string url;     private logger log;     connection connection = null;       resultset resultset = null;       statement statement = null;        public db(final plugin plugin, final string host, final string database,             final string user, final string password) {         this.plugin = plugin;         url = "jdbc:mysql://" + host + "/" + database + "?user=" + user                 + "&password=" + password;         log = plugin.getserver().getlogger();         initdriver("com.mysql.jdbc.driver");     }      public db(final plugin plugin, final string filepath) {         this.plugin = plugin;         url = "jdbc:sqlite:" + new file(filepath).getabsolutepath();         log = plugin.getserver().getlogger();         initdriver("org.sqlite.jdbc");     }      private void initdriver(final string driver) {         try {             class.forname(driver);         } catch (final exception e) {             log.severe("database driver error:" + e.getmessage());         }     }      public int resultint(resultset result, int column) {         if (result == null)             return 0;         try {             result.next();             int integer = result.getint(column);             result.close();              return integer;         } catch (sqlexception e) {             log.severe("database result error: " + e.getmessage());         }         return 0;     }      public string resultstring(resultset result, int column) {         if (result == null)             return null;         try {             result.next();             string string = result.getstring(column);             result.close();             return string;         } catch (sqlexception e) {             log.severe("database result error: " + e.getmessage());         }         return null;     }      public resultset query(final string query) {         return query(query, false);     }      public resultset query(final string query, final boolean retry) {         try {             final connection connection = drivermanager.getconnection(url);             final preparedstatement statement = connection                     .preparestatement(query);             if (statement.execute())                 return statement.getresultset();         } catch (final sqlexception e) {             final string msg = e.getmessage();             log.severe("database query error: " + msg);             if (retry && msg.contains("_busy")) {                 log.severe("retrying query...");                  plugin.getserver().getscheduler()                         .schedulesyncdelayedtask(plugin, new runnable() {                             @override                             public void run() {                                  query(query);                             }                         }, 20);             }         }         return null;     } } 

but don't know how close connection , statement. in code must use

base = new db(this, getdatafolder() + file.separator + "sqlite.db"); 

but using code creates multiple threads , gives me errors. must find class can close connection , statement. learning java few weeks , don't know much.

i suggest read @ least bit of the java tutorials: jdbc basics. pretty explained there...

the short answer is: call resultset.close(), statement.close(), connection.close() after you're done working each object.
or use spring's jdbctemplate, mybatis or hibernate, each adds layer of abstraction (and complexity) on top of aforementioned framework technique.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -