mysql - 我如何使用 select 语句

标签 mysql jdbc

我想从表中使用 select max。我想使用 PreparedStatement。我有一个复合主键,其中包含电视连续剧和 epo 编号。当我添加新的 epo 时,它将用于表并从指南表中获取电视系列代码、所有节目的内容以及每个节目的代码,然后添加到新表中。我希望它通过获取最大值来获取最后一个 epo,然后递增 +1“自动化应用程序”。

那么我如何选择 max where id =?

如果你明白我的意思

   pstm2=con.prepareStatement(max);
   String max="select MAX(epono) as eponoo from archieve wwhere id like ? ";

最佳答案

这个程序会有帮助

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectRecordsUsingPreparedStatement {
  public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "name";
    String password = "password";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static void main(String[] args) {
    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = getConnection();
      String query = "select deptno, deptname, deptloc from dept where deptno > ?";

      pstmt = conn.prepareStatement(query); // create a statement
      pstmt.setInt(1, 1001); // set input parameter
      rs = pstmt.executeQuery();
      // extract data from the ResultSet
      while (rs.next()) {
        int dbDeptNumber = rs.getInt(1);
        String dbDeptName = rs.getString(2);
        String dbDeptLocation = rs.getString(3);
        System.out.println(dbDeptNumber + "\t" + dbDeptName + "\t" + dbDeptLocation);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        rs.close();
        pstmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

关于mysql - 我如何使用 select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965825/

相关文章:

php - laravel中的递归外键

sql - MySQL维护顺序编号 "top n"列表的方法

mysql - 减去总和的结果值

postgresql - 如何限制Postgresql jdbc池连接数?

java - 使用 neo4j-jdbc 驱动程序与 Java 连接 neo4j

java - 选择列并从其他选择中隐藏

Php在循环中获取一个数组

mysql - 如何打印mysql中SUBSTRING_INDEX()字段的数据

java - Java 的 SQL JDBC 拦截器,可以跟踪可疑的 SQL 查询

windows - 数据库故障使用 JDBC 持久性关闭 ActiveMQ windows 服务