下面的代碼塊是我嘗試過的一些代碼。我已經在我的JSP頁面中設置了測試檢查,並且沒有超越3.22,從而結束了程序。這是另一組需要在下面承諾的關係,但我認爲它沒有經過。從ExecuteQuery()得到ResultSet沒有正確執行
我正在使用PostgreSQL。所有被拉的數據都來自HTML表單(基本上是一個註冊表單)。
我得到了第一個INSERT工作的用戶表,它工作得很好。遵循相同的語法,我想我可以拉動創建的用戶標識(它是根據我的數據庫模式中的序列關係自動創建的)。但是,當我試圖從相同的USERS表(從我創建的新創建的元組)中查找用戶標識時,這不起作用,因此我可以使用它創建新的「位置」行。我試着無處不在,而且我沒有想法。我需要將這個塊放在不同的JSP頁面中嗎?一個不同的連接?關閉並重新打開連接?任何想法都會很棒。
謝謝!
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="DbConnection"
class="edu.bu.cs.cs460.photoshare.DbConnection">
<jsp:setProperty name="DbConnection" property="*"/>
</jsp:useBean>
<%
out.println("test check 1");
String email = request.getParameter("email");
String password = request.getParameter("password");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
int dob = Integer.parseInt(request.getParameter("dob"));
out.println("test check 2: " + email + " " + password + " " + fname + " " + lname + " " + dob);
try{
PreparedStatement st_users = null;
Connection myCon = null;
out.println("test check 2.21");
try {
Context ctx = new InitialContext();
if (ctx == null) {
throw new RuntimeException("No Context");
}
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/postgres");
if (ds == null) {
throw new RuntimeException("Datasource not found");
}
myCon = ds.getConnection();
if (myCon == null) {
throw new RuntimeException("Could not get connection");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (NamingException e) {
throw new RuntimeException(e.getMessage());
}
PreparedStatement getUID = null;
getUID=myCon.prepareStatement("SELECT MIN(\"user_id\") FROM \"Users\" as U WHERE \"U.email\" = ?");
getUID.setString(1, email);
out.println("test check 3.22");
ResultSet rs = null;
rs = getUID.executeQuery();
out.println("test check 3.999");
int userid = rs.getInt(1);
getUID.close();
//String tempuserid = (String)rs.getAttribute(1);
//int userid = rs.getInt(1);
//sint userid = Integer.parseInt(tempuserid);
out.println("test check 4: " + userid);
/* now we have the ID from the set */
/* this will set the location tabe now for the registered user */
PreparedStatement st_loc = null;
st_loc=myCon.prepareStatement("INSERT INTO \"Locations\" (\"user_id\", \"curLoc\", \"hcity\", \"hstate\", \"country\") VALUES (?, ?, ?, ?, ?)");
String hcity = request.getParameter("city");
String hstate = request.getParameter("state");
String curLoc = " ";
String country = request.getParameter("country");
out.println("test check 5: " + hcity + " " + hstate + " " + curLoc + " " + country);
st_loc.setInt(1, userid);
st_loc.setString(2, curLoc);
st_loc.setString(3, hcity);
st_loc.setString(4, hstate);
st_loc.setString(5, country);
st_loc.executeUpdate();
out.println("test check 6:");
st_loc.close();
myCon.close();
out.println("Data is successfully inserted into database.");
}
catch(Exception e){
System.out.println(e);
}
%>
我明白你對JSP的看法,而不是收集數據庫查詢。爲了這個項目,我只是瞄準功能,而不是爲了安全和建設。如果我確實這樣做了,會通過Java對象工作嗎?另外,我非常熟悉PHP中的SQL查詢,因此在Java/SQL中執行此操作是不同的。另外,我不知道這些查詢是否重要,但這是PostgreSQL而不是mySQL。 – user1302223 2012-03-30 02:37:58