title: how to insert into database table with different data types using Java SQL
body: <p>I have a Java SQL question relative to the insert method. If I have a database with multiple tables, I want to write a single insert method that can insert a row of data into any selected table for any type of relational database. If some tables contain different data types, for examples (integer, date, varchar, and etc.). My codes:</p>
  
  <pre><code>public void insertData(String tablename, String... values)
          throws SQLException {
  
      Connection          con = null;
      PreparedStatement   prepStmt = null;
  
      if (values.length == 0) {
  
          throw new SQLException("Must supply values");
      }
  
      try{
  
          con = getConnection();
  
          String  sql = "insert into "+tablename+" values(";
  
          for (int i = 0; i &lt; values.length; i++) {
              sql += "?";
              if (i != values.length-1) sql += ",";
          }
  
          sql += ")";
  
          prepStmt = con.prepareStatement(sql);
  
          for (int i = 0; i &lt; values.length; i++) {
              prepStmt.setString(i, values[i]);
          }
          prepStmt.executeUpdate();
      }
      finally {
  
          closeStatement(prepStmt);
          closeConnection(con);
      }
  }
  </code></pre>
  
  <p>For example, use case:</p>
  
  <blockquote>
    <p>Table Teacher has int id, varchar(50) name, int age, text subject, int
    classid; </p>
    
    <p>Table Student has int id, varchar(50) name, date dateofbirth, text
    address;</p>
    
    <p>Table Class has int id, text subject;</p>
  </blockquote>
  
  <p>If I use:</p>
  
  <pre><code>insertData(Teacher, new String[] {"10", "Cass", "32", "Math", "10222"});
  </code></pre>
  
  <p>Will that data insert into the table successfully? I heard the database is only using varchar. If that is the case, then I don't have to worry about the type. If the type is varchar, should I use prepStmt.setString()?</p>
  
  <p>I need to support multiple different types of database, such like SQL Server, MySQL, Oracle and others. Will that one insertData() method work for all different databases?</p>