⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insertupdate.java

📁 我自己在做java训练的时候做的一个小项目
💻 JAVA
字号:
package text;
import java.sql.*;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class InsertUpdate {
    public InsertUpdate() {

    }
    public static int getNewId(){
        int i=0;
        try{
            Connection con = null;
           con = JDBC_ODBC.getConnction();
          String str="select * from Record";
        //  String str="select max(Id) from Record";
          Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          ResultSet rs=stmt.executeQuery(str);
          if(rs.last()==false){
              i=0;
          }else{
              i=rs.getInt(1);
          }



        }catch(SQLException ce){
            System.out.println(ce);
        }
       return  i+1;

    }
    public static void doStartUseComputerBusiness( Record record,Computer computer){

    Connection con = null;
    PreparedStatement pStatement1 = null;
    PreparedStatement pStatement2 = null;

    try {
      con = JDBC_ODBC.getConnction();
           String strSql =
          "insert into record(Id,CardId,ComputerId,BeginTime) values(?,?,?,?)";
      pStatement1 = con.prepareStatement(strSql);
      pStatement1.setInt(1, getNewId());
      pStatement1.setString(2, record.getCardId());
      pStatement1.setString(3, record.getComputerId());
      pStatement1.setString(4, record.getBeginTime());
      pStatement1.executeUpdate();
      String strSql2 =
          "update Computer set OnUse =1 where id =(?) ; ";
      pStatement2 = con.prepareStatement(strSql2);
      pStatement2.setString(1, computer.getId());
      pStatement2.executeUpdate();

    } catch (SQLException sqlE){
      sqlE.printStackTrace();
      try {
        con.rollback();

      } catch (Exception e) {
        e.printStackTrace();
      }
    } finally {
      try {
        pStatement1.close();
        pStatement2.close();
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }

    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -