我如何使用选择陈述

时间:2016-03-13 02:39:39

标签: mysql jdbc

我想从表中使用select max。我想使用PreparedStatement。我有一个复合主键,由t.v系列和环氧树脂编号组成。当我添加新的epo时,它将用于表并从guidline表中获取所有程序的内容和每个代码的t.v系列代码,然后添加到新表中。我希望它通过获得最大值然后增加+1“自动化应用程序”来获得最后一个环氧树脂。

那我怎么能select max where id =?

如果你让我喜欢

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

1 个答案:

答案 0 :(得分:1)

这个程序会有所帮助

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();
      }
    }
  }
}