2013-07-31 74 views
-4

我需要格式化,我從JDBC獲得的結果集,格式結果集中在Java中

是否有我使用任何現成的包,

http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/ResultSetFormatter.html

我看到它在上面的網站,但不知道怎麼弄的jar文件

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 
     { 
      // This will load the MySQL driver, each DB has its own driver 
      Class.forName("com.mysql.jdbc.Driver"); 
      // Setup the connection with the DB 
      connect = DriverManager 
       .getConnection("jdbc:mysql://localhost/feedback?" 
        + "user=sqluser&password=sqluserpw"); 

      connect.setAutoCommit(false); 

      // Statements allow to issue SQL queries to the database 
      statement = connect.createStatement(); 
      // Result set get the result of the 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 into 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 from REGISTRATION"); 
     ddlresultSet = preparedStatement.executeQuery(); 
     writeResultSet(ddlresultSet); 

    } 

    private void updateRows(Connection connect) throws SQLException { 
     preparedStatement = connect 
       .prepareStatement("Update REGISTRATION set first = 'RENAMED' WHERE ID = 1"); 
     dmlresultSet = preparedStatement.executeUpdate(); 
    } 

    private void writeResultSet(ResultSet resultSet) throws SQLException { 
     System.out.println("              "); 
     System.out.println("*****   Printing the Rows     *****"); 
     // ResultSet is initially before the first data set 
     while (resultSet.next()) { 
      // It is possible to get the columns via name 
      // also possible to get the columns via the column number 
      // which starts at 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); 

     } 
     } 


    // You need to close the resultSet 
    private void close() { 
    try { 
     if (ddlresultSet != null) { 
     ddlresultSet.close(); 
     } 

     if (statement != null) { 
     statement.close(); 
     } 

     if (connect != null) { 
     connect.close(); 
     } 
    } catch (Exception e) { 

    } 
    } 
} 
+2

-1的努力 – Ankit

回答

1
如果你想上面的庫(Apache的耶拿)

,使用此鏈接:

http://jena.apache.org/download/index.html

或二進制這裏選擇:

http://www.apache.org/dist/jena/binaries/

+0

LACT我進口的所有jar文件在我的Java項目下載files..The進口進口玉米之後。 hp.hpl.jena.query *。是成功的,但同時在我的程序中引用類ResultSetFormattter時,它的失敗...我的經驗非常少,因此您必須忍受我 – user1050619

+0

@ user1050619它給出了什麼錯誤? – Ankit

+0

@ ay89-它說「ResultSetFormatter」無法解析..我已經附上我的代碼上面 – user1050619