我还是 JSP 或 Java 的新手。我目前正在使用名为Conn.java的现有 Java 代码,如下所示:
package org.myclass.auth;
import java.sql.*;
import org.myclass.global.Variable;
import org.myclass.auth.LoadProps;
import org.apache.commons.dbcp.BasicDataSource;
import javax.sql.DataSource;
public class Conn {
String success = "";
public LoadProps app;
public Variable var = new Variable();
Connection conn = null;
Statement st = null;
ResultSet rs = null;
int x = 0;
public Conn(String path) {
app = new LoadProps(path);
}
public Conn() {
app = new LoadProps(var.getPATH());
}
public boolean open() {
boolean success = false;
try {
DataSource dataSource = setupDataSource(app.getUrl(), app.getUser(), app.getPassword());
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return success;
}
public DataSource setupDataSource(String connectURI, String connectUser, String connectPassword) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername(connectUser);
ds.setPassword(connectPassword);
ds.setUrl(connectURI);
return ds;
}
public static void printDataSourceStats(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
}
public static void shutdownDataSource(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
bds.close();
}
public String getUrl() {
return app.getUrl();
}
public String getUser() {
return app.getUser();
}
public String getPassword() {
return app.getPassword();
}
public void close() throws SQLException {
if(!conn.isClosed()) {
conn.close();
}
}
public ResultSet query(String query) {
try {
st = conn.createStatement();
rs = st.executeQuery(query);
}
catch(SQLException sqle) {
sqle.printStackTrace();
}
return rs;
}
public boolean queryInsert(String query) {
boolean value = false;
try {
st = conn.createStatement();
value = st.execute(query);
value = true;
}
catch(SQLException sqle) {
sqle.printStackTrace();
}
return value;
}
public int queryUpdate(String query) {
int value = 0;
try {
st = conn.createStatement();
value = st.executeUpdate(query);
}
catch(SQLException sqle) {
sqle.printStackTrace();
}
return value;
}
}
为了使用 PreparedStatement,所以稍后我只在我的 JSP 代码上调用它,例如:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="org.myclass.auth.Conn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%
String query = "SELECT adminid FROM administrator where username = '"+ session.getAttribute("username") +"'";
Conn conn = new Conn();
ResultSet rsQ;
conn.open();
rsQ = conn.query(query);
rsQ.next();
int adminid = rsQ.getInt(1);
conn.close();
Conn conn1 = new Conn();
String insert = "INSERT INTO campaign (uid,sender_id,content,Starttime,status,lastmodifiedid,creationtime,msisdnfile) VALUES('"+ adminid +"','"+ request.getParameter("sender_id") +"','"+request.getParameter("message")+"','"+request.getParameter("startTime")+"','Running','"+adminid+"',now(),'file2.txt')";
out.print(insert);
conn1.open();
conn1.queryInsert(insert);
conn1.close();
%>
我应该怎么做来修改 Conn.java,因为当我运行 JSP 时它返回 NULL,当它转到conn1.queryInsert(insert)时?还有一件事,如果我使用 PreparedStatement,我应该怎么做才能让我的 JSP 和上面的一样?提前致谢