import java.sql.*;

public class Mysql {

  public static void main(String[] args) {
    //Select文の作成
    int[][] problem = {
        { 1, 0, 0, 0, 0, 7, 0, 9, 0 },
        { 0, 3, 0, 0, 2, 0, 0, 0, 8 },
        { 0, 0, 9, 6, 0, 0, 5, 0, 0 },
        { 0, 0, 5, 3, 0, 0, 9, 0, 0 },
        { 0, 1, 0, 0, 8, 0, 0, 0, 2 },
        { 6, 0, 0, 0, 0, 4, 0, 0, 0 },
        { 3, 0, 0, 0, 0, 0, 0, 1, 0 },
        { 0, 4, 0, 0, 0, 0, 0, 0, 7 },
        { 0, 0, 7, 0, 0, 0, 3, 0, 0 }};
    String queryString=SqlCreator.createQueryString(problem);
    System.out.println(queryString);

    //MySQLで解く
    final int n = problem.length;
    try {
      //接続
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "test", "pass");
      Statement stmt = conn.createStatement();

      //テーブルの準備
      stmt.execute("DROP TABLE IF EXISTS x");
      stmt.execute("CREATE TABLE x (n INT)");
      for (int i = 1; i <= n; ++i) stmt.execute("INSERT INTO x VALUES (" + i + ")");

      //数独を解く
      ResultSet rs=stmt.executeQuery(queryString);

      //結果の表示
      if (rs.next()) {
        for (int r = 0; r < n; ++r) {
          for (int c = 0; c < n; ++c) {
            String label="v" + (r + 1) + "_" + (c + 1);
            if (problem[r][c] == 0) problem[r][c]=rs.getInt(label);
            System.out.print(problem[r][c]+" ");
          }
          System.out.println();
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

