public static void main(String args[]) {
   try {
     // Step 2: load driver
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     // Step 3: define the connection URL
     String url = "jdbc:odbc:personDSN";
     // Step 4: establish the connection
     Connection con = DriverManager.getConnection(url);
     // Step 5: create PrepareStatement by passing sql and
     // ResultSet appropriate fields
     String sql = "SELECT * FROM Person";
     PreparedStatement pStmt =
         con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
     // Step 6: execute the query
     ResultSet rs = pStmt.executeQuery();
     // moving cursor to insert row
     rs.moveToInsertRow();
     // updating values in insert row
     rs.updateString("Name", "imitiaz");
     rs.updateString("Address", "cantt");
     rs.updateString("phoneNum", "9201211");
     // inserting row in resultset & into database
     rs.insertRow();
     // Step 8: close the connection
     con.close();
   } catch (Exception sqlEx) {
     System.out.println(sqlEx);
   }
 } // end main
Esempio n. 2
0
 public void addToDb(Contact c, Connection con) {
   try (Statement s = con.createStatement();
       ResultSet resultSet = s.executeQuery("SELECT * FROM contact")) {
     resultSet.moveToInsertRow();
     resultSet.updateInt("ID", c.getId());
     resultSet.updateString("name", c.getName());
     if (c.getClass().getName().equals("jdbase.model.Person"))
       resultSet.updateString("surname", ((Person) c).getSurname);
     else resultSet.updateString("surname", " ---- ");
     resultSet.updateString("email", c.getEmail());
     resultSet.updateString("phoneNumber", c.getPhoneNumber());
     resultSet.insertRow();
     System.out.println(" After adding :");
     System.out.println("ID \tfName \tsurname \temail \t\tphoneNo");
     while (resultSet.next()) {
       System.out.println(
           resultSet.getInt("id")
               + "\t"
               + resultSet.getString("name")
               + "t\""
               + resultSet.getString("surname")
               + "t\""
               + resultSet.getString("email")
               + "t\""
               + resultSet.getString("phoneNumber"));
     }
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
  /**
   * Helper function intended to be overwritten by subclasses. Thsi is where the real requiest for
   * IDs happens
   */
  protected void performIDRequest() throws Exception {
    Connection dbConnection = null;

    try {
      try {
        dbConnection = dataSource.getConnection();
        Statement stmt =
            dbConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        ResultSet set = null;
        set =
            stmt.executeQuery(
                "SELECT id, " + dbColumn + " FROM " + dbTable); // $NON-NLS-1$ //$NON-NLS-2$
        if (!set.next()) {
          set.moveToInsertRow();
          set.insertRow();
          set.updateLong(dbColumn, NUM_IDS_GRABBED);
          set.moveToCurrentRow();
          set.next();
        }
        long nextID = set.getLong(dbColumn);
        long upTo = nextID + mCacheQuantity;
        set.updateLong(dbColumn, upTo);
        set.updateRow();
        stmt.close();
        setMaxAllowedID(upTo);
        setNextID(nextID);
      } finally {
        if (dbConnection != null) {
          dbConnection.close();
        }
      }
    } catch (SQLException e) {
      throw new NoMoreIDsException(e);
    }
  }
Esempio n. 4
0
 protected void index(ResultSet rs, ILinguist linguist, long sid, String sent)
     throws LoadException {
   if (rs == null) throw new LoadException("Index ResultSet null");
   try {
     // System.out.println(sent);
     String[] words = linguist.indexkeys(sent);
     if (words.length > 0) {
       Map<String, String> dup = new HashMap<String, String>();
       int i = 0;
       for (String word : words) {
         // System.out.print(word+" ");
         if (!dup.containsKey(word)) {
           dup.put(word, word);
           rs.moveToInsertRow();
           rs.updateString("F_Word", word);
           rs.updateLong("F_SID", sid);
           rs.updateInt("F_Offset", i++);
           rs.insertRow();
         }
       }
       // System.out.println();
     }
   } catch (SQLException se) {
     throw new LoadException("SQLException occurred: " + se.getMessage());
   } catch (LanguageException le) {
     throw new LoadException("LanguageException occurred indexing: " + le.getMessage());
   }
 }
  public static void main(String[] args) {

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    // note that we're doing this the old way - without a try-with-resources block
    try {
      // make a connection to the database
      connection = DriverManager.getConnection(URL + DATABASE, USER, PASSWORD);

      // ensure that we're using transactions
      connection.setAutoCommit(false);

      // create a statement object. make sure the resultset is updateable
      statement =
          connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

      // execute the query
      resultSet = statement.executeQuery("SELECT * FROM Person");

      // prepare to insert a row into the resultset. recall that the resultset can also be used for
      // updates
      resultSet.moveToInsertRow();
      resultSet.updateString("name", "Austin");
      resultSet.updateInt("age", 87);

      // insert the row
      resultSet.insertRow();

      // commit the transaction
      connection.commit();

    } catch (SQLException sqle) {

      // rollback the transaction
      try {
        connection.rollback();
      } catch (SQLException sqle2) {
        sqle2.printStackTrace();
        System.out.println("Transaction was rolled back!");
      }

    } finally {

      // close the resources
      try {
        if (connection != null) connection.close();
        if (statement != null) statement.close();
        if (resultSet != null) resultSet.close();
      } catch (SQLException sqle3) {
      }
    }
  }
Esempio n. 6
0
 public Boolean begin_new_record(String table) {
   try {
     Statement s =
         connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
     s.execute("Select * From " + table + " Where " + table + "_id = 0");
     if (results != null) results.close();
     results = s.getResultSet();
     results.moveToInsertRow();
     current_table = table;
     error = false;
     return true;
   } catch (Exception ex) {
     throw new InvalidQueryException("Database update failed");
   }
 }