コード例 #1
0
  public static int executeUpdate(String query) {
    int rowsAffected = 0;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      String dbpwd = Utilfunctions.getDbConfig("password");
      if (con == null)
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/schedulepro", "root", dbpwd);
      PreparedStatement statement;
      statement = con.prepareStatement(query);

      rowsAffected = statement.executeUpdate();
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, e);
    }
    return rowsAffected;
  }
コード例 #2
0
  public static int insertWithGeneratedKey(String query) {
    int lastInsertId = 0;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      String dbpwd = Utilfunctions.getDbConfig("password");
      if (con == null)
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/schedulepro", "root", dbpwd);

      PreparedStatement statement = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
      statement.executeUpdate();

      ResultSet keys = statement.getGeneratedKeys();
      keys.next();
      lastInsertId = keys.getInt(1);

      return lastInsertId;
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, e);
    }
    return lastInsertId;
  }
コード例 #3
0
  public static ResultSet executeQuery(String query) {

    try {
      try {
        Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      String dbpwd = Utilfunctions.getDbConfig("password");
      if (con == null)
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/schedulepro", "root", dbpwd);

      PreparedStatement statement = con.prepareStatement(query);
      result = statement.executeQuery();

    } catch (SQLException e) {
      // JOptionPane.showMessageDialog(null, e.getMessage());
      Logger.getLogger(DayWisePeriodConfigChooseForm.class.getName()).log(Level.SEVERE, null, e);
    }

    return result;
  }