• <small id='IaiXr'></small><noframes id='IaiXr'>

    <i id='IaiXr'><tr id='IaiXr'><dt id='IaiXr'><q id='IaiXr'><span id='IaiXr'><b id='IaiXr'><form id='IaiXr'><ins id='IaiXr'></ins><ul id='IaiXr'></ul><sub id='IaiXr'></sub></form><legend id='IaiXr'></legend><bdo id='IaiXr'><pre id='IaiXr'><center id='IaiXr'></center></pre></bdo></b><th id='IaiXr'></th></span></q></dt></tr></i><div id='IaiXr'><tfoot id='IaiXr'></tfoot><dl id='IaiXr'><fieldset id='IaiXr'></fieldset></dl></div>
  • <legend id='IaiXr'><style id='IaiXr'><dir id='IaiXr'><q id='IaiXr'></q></dir></style></legend>

        <tfoot id='IaiXr'></tfoot>
          <bdo id='IaiXr'></bdo><ul id='IaiXr'></ul>
      1. 如何将整数转换为整数?

        How to convert Integer to int?(如何将整数转换为整数?)
        <tfoot id='QOXtk'></tfoot>

          <tbody id='QOXtk'></tbody>
            <bdo id='QOXtk'></bdo><ul id='QOXtk'></ul>
          • <small id='QOXtk'></small><noframes id='QOXtk'>

              <legend id='QOXtk'><style id='QOXtk'><dir id='QOXtk'><q id='QOXtk'></q></dir></style></legend>

            1. <i id='QOXtk'><tr id='QOXtk'><dt id='QOXtk'><q id='QOXtk'><span id='QOXtk'><b id='QOXtk'><form id='QOXtk'><ins id='QOXtk'></ins><ul id='QOXtk'></ul><sub id='QOXtk'></sub></form><legend id='QOXtk'></legend><bdo id='QOXtk'><pre id='QOXtk'><center id='QOXtk'></center></pre></bdo></b><th id='QOXtk'></th></span></q></dt></tr></i><div id='QOXtk'><tfoot id='QOXtk'></tfoot><dl id='QOXtk'><fieldset id='QOXtk'></fieldset></dl></div>

                1. 本文介绍了如何将整数转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个 Web 应用程序,在该应用程序中,数据将在客户端和客户端之间传输.服务器端.

                  I am working on a web application in which data will be transfer between client & server side.

                  我已经知道 JavaScript int != Java int.因为,Java int 不能为空,对.现在这是我面临的问题.

                  I already know that JavaScript int != Java int. Because, Java int cannot be null, right. Now this is the problem I am facing.

                  我将我的 Java int 变量更改为 Integer.

                  I changed my Java int variables into Integer.

                  public void aouEmployee(Employee employee) throws SQLException, ClassNotFoundException
                  {
                     Integer tempID = employee.getId();
                     String tname = employee.getName();
                     Integer tage = employee.getAge();
                     String tdept = employee.getDept();
                     PreparedStatement pstmt;
                     Class.forName("com.mysql.jdbc.Driver");
                     String url ="jdbc:mysql://localhost:3306/general";
                     java.sql.Connection con = DriverManager.getConnection(url,"root", "1234");
                     System.out.println("URL: " + url);
                     System.out.println("Connection: " + con);
                     pstmt = (PreparedStatement) con.prepareStatement("REPLACE INTO PERSON SET ID=?, NAME=?, AGE=?, DEPT=?");
                     pstmt.setInt(1, tempID);
                     pstmt.setString(2, tname);
                     pstmt.setInt(3, tage);
                     pstmt.setString(4, tdept);
                     pstmt.executeUpdate();
                   }
                  

                  我的问题在这里:

                  pstmt.setInt(1, tempID);
                  
                  pstmt.setInt(3, tage);
                  

                  我不能在这里使用整数变量.我试过 intgerObject.intValue();但它使事情变得更加复杂.我们还有其他转换方法或转换技术吗?

                  I cant use the Integer variables here. I tried with intgerObject.intValue(); But it makes things more complex. Do we have any other conversion methods or conversion techniques?

                  任何修复都会更好.

                  推荐答案

                  正如已经在别处写的:

                  • 对于 Java 1.5 及更高版本,您(几乎)不需要做任何事情,它由编译器完成.
                  • 对于 Java 1.4 及之前版本,使用 Integer.intValue() 将 Integer 转换为 int.
                  • For Java 1.5 and later you don't need to do (almost) anything, it's done by the compiler.
                  • For Java 1.4 and before, use Integer.intValue() to convert from Integer to int.

                  但是正如您所写,Integer 可以为空,因此在尝试转换为 int 之前检查一下是明智的(否则可能会遇到 NullPointerException).

                  BUT as you wrote, an Integer can be null, so it's wise to check that before trying to convert to int (or risk getting a NullPointerException).

                  pstmt.setInt(1, (tempID != null ? tempID : 0));  // Java 1.5 or later
                  

                  pstmt.setInt(1, (tempID != null ? tempID.intValue() : 0));  // any version, no autoboxing  
                  

                  * 使用默认值零,也可以什么都不做,显示警告或...

                  我大多不喜欢使用自动装箱(第二个示例行),所以很清楚我想要做什么.

                  I mostly prefer not using autoboxing (second sample line) so it's clear what I want to do.

                  这篇关于如何将整数转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                  Unexpected result in long/int division(意外结果导致长/整数除法)
                  <tfoot id='nxqKE'></tfoot>
                  <legend id='nxqKE'><style id='nxqKE'><dir id='nxqKE'><q id='nxqKE'></q></dir></style></legend>

                    <i id='nxqKE'><tr id='nxqKE'><dt id='nxqKE'><q id='nxqKE'><span id='nxqKE'><b id='nxqKE'><form id='nxqKE'><ins id='nxqKE'></ins><ul id='nxqKE'></ul><sub id='nxqKE'></sub></form><legend id='nxqKE'></legend><bdo id='nxqKE'><pre id='nxqKE'><center id='nxqKE'></center></pre></bdo></b><th id='nxqKE'></th></span></q></dt></tr></i><div id='nxqKE'><tfoot id='nxqKE'></tfoot><dl id='nxqKE'><fieldset id='nxqKE'></fieldset></dl></div>
                        <bdo id='nxqKE'></bdo><ul id='nxqKE'></ul>
                          <tbody id='nxqKE'></tbody>

                            <small id='nxqKE'></small><noframes id='nxqKE'>