基本上,我的Web应用程序中有两个类,一个“ FormProcess.java”类,它将信息插入到HTML表单中,执行一些计算,然后将结果输出为HTML。这绝对好用。
我还有一个用于将数据添加到MySQL数据库的新行的类,该类称为“ SqlConnect.java”。如果我在服务器上单独运行它,这也可以正常工作(即,它正确地在表上插入了一行)。
我正在努力使SqlConnect方法从FormProcess类运行。当我将以下内容添加到FormProcess时:
private SqlConnect sql;
并在方法中:
sql.doPost(request, response);
我收到以下错误:
java.lang.NullPointerException
crunch.FormProcess.doGet(FormProcess.java:27)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
此刻是SqlConnect类(我省略了导入等):
@WebServlet(name="sql",urlPatterns={"/sql"})
public class SqlConnect extends javax.servlet.http.HttpServlet {
FormProcess process = new FormProcess();
private static final long serialVersionUID = 1L;
public SqlConnect() {
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
performTask(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
performTask(request, response);
}
public void performTask(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
testJndiDataSource();
}
public void testJndiDataSource() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/CrunchDB");
conn = ds.getConnection();
st = conn.createStatement();
st.executeUpdate("INSERT INTO log " + "VALUES (20, '1', '1', " + process.salaryInt + ", "+ process.takeHomePAYE +", "+ process.takeHomeContractor +", 2)");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
}
我敢肯定有一种简单的方法可以解决这个问题,因为我说SqlConnect类本身可以很好地运行。
干杯
最佳答案
不幸的是,您现在拥有的类结构存在缺陷。 SqlConnect
不应是HttpServlet
的子类,因为人们永远不应该像调用网页一样直接调用它。取而代之的是,只有FormProcess
和您的其他实际servlet应该访问SqlConnect
。
所以现在,您的代码应如下所示:
public void testJndiDataSource(int salaryInt, double takeHomePAYE, String takeHomeContractor) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/CrunchDB");
conn = ds.getConnection();
st = conn.createStatement();
st.executeUpdate("INSERT INTO log " + "VALUES (20, '1', '1', " + process.salaryInt + ", "+ process.takeHomePAYE +", "+ process.takeHomeContractor +", 2)");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
然后,在
FormProcess
中:public void performTask(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int salaryInt = //extract from form...
double takeHomePAYE = //extract from form...
String takeHomeContractor = //extract from form...
SqlConnect sqlConnect = new SqlConnect();
sqlConnect.testJndiDataSource(salaryInt, takeHomePAYE, takeHomeContractor);
}
这是对网站的工作方式进行建模的更合乎逻辑的方法。注意:目前的查询完全容易受到SQL注入的攻击。按照此处的说明将
Statement
更改为PreparedStatement
:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html关于java - Java/MySQL:从Java Web应用程序(TomCat v7.0)将行插入表中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19211798/