0

我有这两页,在我将数据输入表单并提交后,它写道:

Založení letadla
java.lang.NullPointerException

这个问题我能做些什么......因为可以用谷歌搜索的用户编写的手册对我没有帮助:(

管理员.jsp:

<html>
<head>

</head>
<body>
<form method="post" action="vlozit.jsp">
<p>Založit Letadlo</p><br>
<table>
<tr><td>ID:</td>           <td><input type="text" name="id"></td></tr>
<tr><td>Model letadla:</td><td><input type="text" name="model"></td></tr>
<tr><td>Počet sedadel:</td><td><input type="text" name="sedadel"></td></tr>
</table>
<input type="submit" value="Založit letadlo">

</form>
</body>

和 vlozit.php

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<HTML>
<HEAD>
    <TITLE>Administrace</TITLE>
</HEAD>

<BODY>
    <H1>Založení letadla</H1>

    <%




String ID= request.getParameter("id");
String model = request.getParameter("model");
String sedadel = request.getParameter("sedadel");

try{



        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


        Connection conn = DriverManager.getConnection("jdbc:odbc:ATBS", "CENTOS", "Shaman123");
if(conn != null){


        Statement statement = conn.createStatement();

        String command = "INSERT INTO letadlo (ID, model, sedadel) VALUES ('"+ID+"', '"+model+"', '"+sedadel+"')";
        PreparedStatement ps = conn.prepareStatement(command);
        if(ps != null){
        ps.execute();}}

conn.close();
}
catch(Exception e)
{
out.println(e); 
}



    %>
</BODY>

4

1 回答 1

0

在调用它的方法之前,您应该检查 Connection 对象是否为空。

 Connection conn = DriverManager.getConnection("jdbc:odbc:ATBS", "CENTOS", "Shaman123");

if(conn != null)
{
        Statement statement = conn.createStatement();

        String command = "INSERT INTO letadlo (ID, model, sedadel) VALUES ('"+ID+"', '"+model+"', '"+sedadel+"')";
        PreparedStatement ps = conn.prepareStatement(command);

        ps.execute();

        conn.close();     //Ideally it should be closed inside finally.
}

理想情况下ConnectionPrepareStatement应该在finally块内关闭。

于 2013-07-30T18:25:47.270 回答