首页 > 解决方案 > 将数据插入数据库的这段代码抛出异常。帮助我了解此代码。我是 StackOverflow 的新手

问题描述

<%
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
       
       

           String o=request.getParameter("Order_ID");
           String c=request.getParameter("Customer_Name");
           String c2=request.getParameter("Customer_ID");
           String o2=request.getParameter("Order_Amount");
           String n=request.getParameter("Notes");
           String o3=request.getParameter("Order_Date");

 String connectionURL = "jdbc:mysql://localhost:3306/winter_internship";

 PreparedStatement pstatement = null;
     // Load JBBC driver "com.mysql.jdbc.Driver"
 Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructor().newInstance();
      int updateQuery = 0;
 
     
 if(o!=null && c!=null && c2!=null && o2!=null && n!=null && o3!=null){
         // check if the text box having only blank spaces
     if(o!="" && c!="" && c2!="" && o2!="" && n!="" && o3!="") {
                 try {
          
          connection = DriverManager.getConnection(connectionURL, "root", "misspooh2068");
                        // sql query to insert values in the secified table.
          String queryString = "insert into order_details(Order_ID,Customer_Name,Customer_ID,Order_Amount,Notes,Order_Date) values(?,?,?,?,?,?)";
                  
          pstatement = connection.prepareStatement(queryString);
                  pstatement.setString(1, o);
                  pstatement.setString(2, c);
                  pstatement.setString(3, c2);
                  pstatement.setString(4, o2);
                  pstatement.setString(7, n);
                  pstatement.setString(8, o3);
          updateQuery = pstatement.executeUpdate();
                        if (updateQuery != 0) { %>
           <br>
           <TABLE style="background-color: #E3E4FA;" 
               WIDTH="30%" border="1">
          <tr><th>Data is inserted successfully 
                in database.</th></tr>
       </table>
          <%
          }
        } 
        catch (Exception ex) {
        out.println("Unable to connect to database.");

           }
        finally {
            // close all the connections.
            pstatement.close();
            connection.close();
        }
  }
}
%>

我的数据库截图

当我运行此代码时,它会引发异常并返回无法连接到数据库

关于异常抛出的图像

试图找到许多解决方案,但不断抛出异常。我是 StackOverflow 的新手。

干杯伙计们!帮帮我吧伙计们:)

标签: mysqljspjdbc

解决方案


推荐阅读