format Resultset in Java -
i need format resultset jdbc,
is there readymade package use,
http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/resultsetformatter.html
i see in above website not sure how jar files
package mysql.first; import com.hp.hpl.jena.*; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.date; public class mysqlaccess { private connection connect = null; private statement statement = null; private preparedstatement preparedstatement = null; private int dmlresultset = 0; private resultset ddlresultset = null; public static void main(string[] args) throws exception { mysqlaccess dao = new mysqlaccess(); //dao.readdatabase(); dao.createtemptable(); } public void createtemptable() throws exception{ try { // load mysql driver, each db has own driver class.forname("com.mysql.jdbc.driver"); // setup connection db connect = drivermanager .getconnection("jdbc:mysql://localhost/feedback?" + "user=sqluser&password=sqluserpw"); connect.setautocommit(false); // statements allow issue sql queries database statement = connect.createstatement(); // result set result of sql query string sql = "create temporary table registration " + "(id integer not null, " + " first varchar(255), " + " last varchar(255) )"; dmlresultset = statement .executeupdate(sql); resultsetformattter.out(dmlresultset); system.out.println("temp table created return code = " + dmlresultset); insertrows(1,"tom","leo", connect); selectrows(connect); insertrows(2,"bran","ley", connect); selectrows(connect); updaterows(connect); selectrows(connect); insertrows(3,"di","roby", connect); } catch (exception e){ throw e; } finally{ close(); } } private void insertrows(int id, string first, string last, connection connect) throws sqlexception { preparedstatement = connect .preparestatement("insert registration values (?, ?, ?)"); preparedstatement.setstring(2, first); preparedstatement.setstring(3, last); preparedstatement.setint(1, id); preparedstatement.executeupdate(); } private void selectrows(connection connect) throws sqlexception { preparedstatement = connect .preparestatement("select id, first, last registration"); ddlresultset = preparedstatement.executequery(); writeresultset(ddlresultset); } private void updaterows(connection connect) throws sqlexception { preparedstatement = connect .preparestatement("update registration set first = 'renamed' id = 1"); dmlresultset = preparedstatement.executeupdate(); } private void writeresultset(resultset resultset) throws sqlexception { system.out.println(" "); system.out.println("***** printing rows *****"); // resultset before first data set while (resultset.next()) { // possible columns via name // possible columns via column number // starts @ 1 // e.g. resultset.getstring(2); int id = resultset.getint("id"); string first = resultset.getstring("first"); string last = resultset.getstring("last"); system.out.println("id: " + id); system.out.println("first: " + first); system.out.println("last: " + last); } } // need close resultset private void close() { try { if (ddlresultset != null) { ddlresultset.close(); } if (statement != null) { statement.close(); } if (connect != null) { connect.close(); } } catch (exception e) { } } }
if want above library (apache jena), use link:
http://jena.apache.org/download/index.html
or select binaries here:
Comments
Post a Comment