/// <exception cref="Java.Sql.SQLException"/>
        public static IList <Pair <string, long> > GetCounts(ICollection <string> strs)
        {
            Connect();
            IList <Pair <string, long> > counts = new List <Pair <string, long> >();
            string query = string.Empty;

            foreach (string str in strs)
            {
                str = str.Trim();
                int    ngram = str.Split("\\s+").Length;
                string table = tablenamePrefix + ngram;
                if (!ExistsTable(table))
                {
                    counts.Add(new Pair(str, (long)-1));
                    continue;
                }
                string phrase = EscapeString(str);
                query += "select count from " + table + " where phrase='" + phrase + "';";
            }
            if (query.IsEmpty())
            {
                return(counts);
            }
            IPreparedStatement stmt       = connection.PrepareStatement(query);
            bool                 isresult = stmt.Execute();
            IResultSet           rs;
            IEnumerator <string> iter = strs.GetEnumerator();

            do
            {
                rs = stmt.GetResultSet();
                string ph = iter.Current;
                if (rs.Next())
                {
                    counts.Add(new Pair(ph, rs.GetLong("count")));
                }
                else
                {
                    counts.Add(new Pair(ph, (long)-1));
                }
                isresult = stmt.GetMoreResults();
            }while (isresult);
            System.Diagnostics.Debug.Assert((counts.Count == strs.Count));
            return(counts);
        }
Beispiel #2
0
 public override void AddPatterns(string id, IDictionary <int, ICollection <E> > p)
 {
     try
     {
         IPreparedStatement pstmt = null;
         IConnection        conn  = null;
         conn  = SQLConnection.GetConnection();
         pstmt = GetPreparedStmt(conn);
         AddPattern(id, p, pstmt);
         pstmt.Execute();
         conn.Commit();
         pstmt.Close();
         conn.Close();
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
Beispiel #3
0
        public void Connect()
        {
            Java.Sql.IConnection con = null;
            try {
                var    driver     = new Net.Sourceforge.Jtds.Jdbc.Driver();
                String username   = "******";
                String password   = "******";
                String address    = "192.168.1.101";
                String port       = "1433";
                String database   = "Database";
                String connString = String.Format("jdbc:jtds:sqlserver://{0}:{1}/{2};user={3};password={4}",
                                                  address, port, database, username, password);
                con = DriverManager.GetConnection(connString, username, password);
                IPreparedStatement stmt = null;
                try {
                    //Prepared statement
                    stmt = con.PrepareStatement("SELECT * FROM Users WHERE Id = ? AND Name = ?");
                    stmt.SetLong(1, 1);
                    stmt.SetString(2, "John Doe");
                    stmt.Execute();

                    RunOnUiThread(() => Toast.MakeText(this, "SUCCESS!", ToastLength.Short).Show());

                    IResultSet rs = stmt.ResultSet;

                    IResultSetMetaData rsmd = rs.MetaData;
                    PrintColumnTypes.PrintColTypes(rsmd);
                    Console.WriteLine("");

                    int numberOfColumns = rsmd.ColumnCount;

                    for (int i = 1; i <= numberOfColumns; i++)
                    {
                        if (i > 1)
                        {
                            Console.Write(",  ");
                        }
                        String columnName = rsmd.GetColumnName(i);
                        Console.Write(columnName);
                    }
                    Console.WriteLine("");
                    while (rs.Next())
                    {
                        for (int i = 1; i <= numberOfColumns; i++)
                        {
                            if (i > 1)
                            {
                                Console.Write(",  ");
                            }
                            String columnValue = rs.GetString(i);
                            Console.Write(columnValue);
                        }
                        Console.WriteLine("");
                    }

                    stmt.Close();
                    con.Close();
                } catch (Exception e) {
                    RunOnUiThread(() => Toast.MakeText(this, e.Message, ToastLength.Long).Show());
                    Console.WriteLine(e.StackTrace);
                    stmt.Close();
                }
                con.Close();
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
                RunOnUiThread(() => Toast.MakeText(this, e.Message, ToastLength.Long).Show());
            }
            RunOnUiThread(() => _button.Enabled = true);
        }
Beispiel #4
0
 //
 //  @Override
 //  public ConcurrentHashIndex<SurfacePattern> readPatternIndex(String dir){
 //    //dir parameter is not used!
 //    try{
 //      Connection conn = SQLConnection.getConnection();
 //      //Map<Integer, Set<Integer>> pats = new ConcurrentHashMap<Integer, Set<Integer>>();
 //      String query = "Select index from " + patternindicesTable + " where tablename=\'" + tableName + "\'";
 //      Statement stmt = conn.createStatement();
 //      ResultSet rs = stmt.executeQuery(query);
 //      ConcurrentHashIndex<SurfacePattern> index = null;
 //      if(rs.next()){
 //        byte[] st = (byte[]) rs.getObject(1);
 //        ByteArrayInputStream baip = new ByteArrayInputStream(st);
 //        ObjectInputStream ois = new ObjectInputStream(baip);
 //        index  = (ConcurrentHashIndex<SurfacePattern>) ois.readObject();
 //      }
 //      assert index != null;
 //      return index;
 //    }catch(SQLException e){
 //      throw new RuntimeException(e);
 //    } catch (ClassNotFoundException e) {
 //      throw new RuntimeException(e);
 //    } catch (IOException e) {
 //      throw new RuntimeException(e);
 //    }
 //  }
 //
 //  @Override
 //  public void savePatternIndex(ConcurrentHashIndex<SurfacePattern> index, String file) {
 //    try {
 //      createUpsertFunctionPatternIndex();
 //      Connection conn = SQLConnection.getConnection();
 //      PreparedStatement  st = conn.prepareStatement("select upsert_patternindex(?,?)");
 //      st.setString(1,tableName);
 //      ByteArrayOutputStream baos = new ByteArrayOutputStream();
 //      ObjectOutputStream oos = new ObjectOutputStream(baos);
 //      oos.writeObject(index);
 //      byte[] patsAsBytes = baos.toByteArray();
 //      ByteArrayInputStream bais = new ByteArrayInputStream(patsAsBytes);
 //      st.setBinaryStream(2, bais, patsAsBytes.length);
 //      st.execute();
 //      st.close();
 //      conn.close();
 //      System.out.println("Saved the pattern hash index for " + tableName + " in DB table " + patternindicesTable);
 //    }catch (SQLException e){
 //      throw new RuntimeException(e);
 //    } catch (IOException e) {
 //      throw new RuntimeException(e);
 //    }
 //  }
 //batch processing below is copied from Java Ranch
 //TODO: make this into an iterator!!
 public override IDictionary <string, IDictionary <int, ICollection <E> > > GetPatternsForAllTokens(ICollection <string> sampledSentIds)
 {
     try
     {
         IDictionary <string, IDictionary <int, ICollection <E> > > pats = new Dictionary <string, IDictionary <int, ICollection <E> > >();
         IConnection          conn          = SQLConnection.GetConnection();
         IEnumerator <string> iter          = sampledSentIds.GetEnumerator();
         int totalNumberOfValuesLeftToBatch = sampledSentIds.Count;
         while (totalNumberOfValuesLeftToBatch > 0)
         {
             int batchSize = SingleBatch;
             if (totalNumberOfValuesLeftToBatch >= LargeBatch)
             {
                 batchSize = LargeBatch;
             }
             else
             {
                 if (totalNumberOfValuesLeftToBatch >= MediumBatch)
                 {
                     batchSize = MediumBatch;
                 }
                 else
                 {
                     if (totalNumberOfValuesLeftToBatch >= SmallBatch)
                     {
                         batchSize = SmallBatch;
                     }
                 }
             }
             totalNumberOfValuesLeftToBatch -= batchSize;
             StringBuilder inClause = new StringBuilder();
             for (int i = 0; i < batchSize; i++)
             {
                 inClause.Append('?');
                 if (i != batchSize - 1)
                 {
                     inClause.Append(',');
                 }
             }
             IPreparedStatement stmt = conn.PrepareStatement("select sentid, patterns from " + tableName + " where sentid in (" + inClause.ToString() + ")");
             for (int i_1 = 0; i_1 < batchSize && iter.MoveNext(); i_1++)
             {
                 stmt.SetString(i_1 + 1, iter.Current);
             }
             // or whatever values you are trying to query by
             stmt.Execute();
             IResultSet rs = stmt.GetResultSet();
             while (rs.Next())
             {
                 string sentid             = rs.GetString(1);
                 byte[] st                 = (byte[])rs.GetObject(2);
                 ByteArrayInputStream baip = new ByteArrayInputStream(st);
                 ObjectInputStream    ois  = new ObjectInputStream(baip);
                 pats[sentid] = (IDictionary <int, ICollection <E> >)ois.ReadObject();
             }
         }
         conn.Close();
         return(pats);
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }