Java JDBC MySQL Connection getAllNames()

Here you can find the source of getAllNames()

Description

get All Names

License

Open Source License

Declaration

public static void getAllNames() throws SQLException 

Method Source Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.TreeSet;

public class Main {
    static TreeSet<Integer> restrictAnalysisToTaxonIDs;

    public static void getAllNames() throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/joerg", "joerg", "joerg9");

        //      Map<String, Integer> name2geneIdCount = new HashMap<String, Integer>();

        Statement statement = connection.createStatement();
        ResultSet resultSet = null;

        for (int taxon : restrictAnalysisToTaxonIDs) {

            String query = "SELECT g.ID, g.name FROM GR_Names AS g";
            //if (restrictAnalysisToTaxonIDs != null && restrictAnalysisToTaxonIDs.size() > 0)
            //   query += ", GR_Origin as o WHERE o.taxon IN " +
            //   restrictAnalysisToTaxonIDs.toString().replaceFirst("\\[", "(").replaceFirst("\\]", ")");
            query += " , GR_Origin as o WHERE g.id=o.id AND o.taxon=" + taxon;

            resultSet = statement.executeQuery(query);
            //ResultSet resultSet = statement.executeQuery("SELECT gene.ID, gene.name FROM GR_Names gene");
            while (resultSet.next()) {
                Integer geneId = resultSet.getInt(1);
                String geneName = resultSet.getString(2);
                //String proteinName = normalize(resultSet.getString(3));

                //         if (name2geneIdCount.containsKey(geneName)) {
                //            int ids = name2geneIdCount.get(geneName) + 1;
                //            name2geneIdCount.put(geneName, ids);
                //         } else
                //            name2geneIdCount.put(geneName, 1);

                //         geneIds = name2geneIdCount.get(proteinName);
                //         if(geneIds==null){
                //            geneIds = new HashSet<Integer>();
                //            name2geneIdCount.put(proteinName, geneIds);
                //         }
                //         geneIds.add(geneId);

                System.out.println(geneId + "\t" + geneName);
            }/*from  www  .j  a va2 s.  co  m*/

            query = "SELECT g.ID, g.name FROM GR_ProteinNames AS g";
            //if (restrictAnalysisToTaxonIDs != null && restrictAnalysisToTaxonIDs.size() > 0)
            //   query += ", GR_Origin as o WHERE o.taxon IN " +
            //   restrictAnalysisToTaxonIDs.toString().replaceFirst("\\[", "(").replaceFirst("\\]", ")");
            query += " , GR_Origin as o WHERE g.id=o.id AND o.taxon=" + taxon;

            resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                Integer geneId = resultSet.getInt(1);
                //String geneName = normalize(resultSet.getString(2));
                String proteinName = resultSet.getString(2);

                //         Set<Integer> geneIds = name2geneIdCount.get(geneName);
                //         if(geneIds==null){
                //            geneIds = new HashSet<Integer>();
                //            name2geneIdCount.put(geneName, geneIds);
                //         }
                //         geneIds.add(geneId);

                //         if (name2geneIdCount.containsKey(proteinName)) {
                //            int ids = name2geneIdCount.get(proteinName) + 1;
                //            name2geneIdCount.put(proteinName, ids);
                //         } else
                //            name2geneIdCount.put(proteinName, 1);

                System.out.println(geneId + "\t" + proteinName);
            }
        } // for all taxa

        resultSet.close();
        statement.close();
        connection.close();

        //      int averageIdsPerName = 0;
        //      for (String name : name2geneIdCount.keySet()) {
        //         Integer idCount = name2geneIdCount.get(name);
        //         averageIdsPerName += idCount;
        //        }

        //System.out.println( "Average Gene IDs per Name: "+ ((double)averageIdsPerName / (double)name2geneIdCount.size()) );

        //System.out.println("finished");
    }
}

Related

  1. createConnection(String hostname, String port, String database, String username, String password)
  2. createConnection(String url, String user, String password)
  3. createConnectionUtf8(String connString)
  4. createTable(String dbFile, String tableName, String tableDefinition)
  5. deleteTableData(String tableName)
  6. getConnection()
  7. getConnection()
  8. getConnection()
  9. getConnection()