title: Exporting database to csv file with java
body: <p>so i found this code on the internet, basically what supposedly it can do is backup all the tables from a db, my question is on this line: </p>
  
  <pre><code>res = st.executeQuery("select * from xcms." + tableName);
  </code></pre>
  
  <blockquote>
    <p>i get the following excpetion exception: SQLException information</p>
  </blockquote>
  
  <p>what does <code>xcms</code>. stands for? what else can i put here?</p>
  
  <pre><code>res = st.executeQuery("select * from " + tableName);
  </code></pre>
  
  <p>btw if i erase xcms. and put it like this ^, i can save only the first table not all the tables, thx</p>
  
  <p>the sourcecode webpage: </p>
  
  <p><a href="https://gauravmutreja.wordpress.com/2011/10/13/exporting-your-database-to-csv-file-in-java/#comment-210" rel="nofollow noreferrer">https://gauravmutreja.wordpress.com/2011/10/13/exporting-your-database-to-csv-file-in-java/#comment-210</a></p>
  
  <pre><code>    public static void main(String[] args) {
          Connection con = null;
          String url = "jdbc:mysql://localhost:3306/";
          String db = "gg";
          String driver = "com.mysql.jdbc.Driver";
          String user = "root";
          String pass = "";
          FileWriter fw;
  
          try {
              Class.forName(driver);
              con = DriverManager.getConnection(url + db, user, pass);
              Statement st = con.createStatement();
              ResultSet res = st.executeQuery("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'gg'");
              List&lt;String&gt; tableNameList = new ArrayList&lt;String&gt;();
              while (res.next()) {
                  tableNameList.add(res.getString(1));
              }
              String filename = "C:\\Users\\Angel Silva\\Documents";
              for (String tableName : tableNameList) {
                  int k = 0;
                  int j = 1;
                  System.out.println(tableName);
                  List&lt;String&gt; columnsNameList = new ArrayList&lt;String&gt;();
                  res = st.executeQuery("select * from " + tableName);
  
                  int columnCount = getColumnCount(res);
  
                  try {
                       fw = new FileWriter("C:\\Users\\Angel Silva\\Documents\\popo1121.csv");
  
                      for (int i = 1; i &lt;= columnCount; i++) {
                          fw.append(res.getMetaData().getColumnName(i));
                          fw.append(",");
  
                      }
                      fw.append(System.getProperty("line.separator"));
  
                      while (res.next()) {
                          for (int i = 1; i &lt;= columnCount; i++) {
                              if (res.getObject(i) != null) {
                                  String data = res.getObject(i).toString();
                                  fw.append(data);
                                  fw.append(",");
                              } else {
                                  String data = "null";
                                  fw.append(data);
                                  fw.append(",");
                              }
                          }
                          fw.append(System.getProperty("line.separator"));
                      }
                      fw.flush();
                      fw.close();
                  } catch (IOException ioe) {
                      ioe.printStackTrace();
                  }
              }
              con.close();
          } catch (ClassNotFoundException cnfe) {
              System.err.println("Could not load JDBC driver");
              cnfe.printStackTrace();
          }catch (SQLException sqle) {System.err.println("SQLException information");}
      }
  
      public static int getRowCount(ResultSet res) throws SQLException {
          res.last();
          int numberOfRows = res.getRow();
          res.beforeFirst();
          return numberOfRows;
      }
      public static int getColumnCount(ResultSet res) throws SQLException {
          return res.getMetaData().getColumnCount();
      }
  }
  </code></pre>