主题:jsp连接数据库问题
我连的是Oracle,我这有两个表,一个是流言表computer,一个是回复表Rcomputer,
create table computer(
title varchar2(30),
matter varchar2(2000)
);
create table Rcomputer(
rtitle varchar(30),
rmatter varchar(2000)
);
这是我第一个文件从数据库里读两个表的代码,
<%
request.setCharacterEncoding("gb2312");
String strUsertitle=request.getParameter("title");
String matter="";
String server="127.0.0.1";
String TITLE="";
String reply="";
String rtitle="";
String rmatter="";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ResultSet rs1 = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:oracle:thin:@"+server+":1521:CONG";
conn=DriverManager.getConnection(url,"system","system");
stmt=conn.createStatement();
rs = stmt.executeQuery("select title,matter from computer");
while(rs.next())
{
TITLE=rs.getString("title");
if(TITLE.equals(strUsertitle))
{
matter=rs.getString("matter");
out.println("<font color = 'Blue'>"+"标题:"+TITLE+"<br>");
out.println(matter+"<br>");
break;
}
}
}catch(SQLException e){
out.println(e.getMessage());
}
rs1 = stmt.executeQuery("select rtitle,rmatter from Rcomputer");
while(rs1.next())
{
rtitle=rs1.getString(rtitle);
if(rtitle.equals(strUsertitle))
{
reply="回复:"+rtitle;
out.println("标题:"+rtitle+"<br>");
out.println(rmatter+"<br>");
}
}
stmt.close();
conn.close();
%>
这是我的另一个王Rcomputer里写东西的代码:
<%
request.setCharacterEncoding("gb2312");
String strUserRtitle = request.getParameter("rtitle");
String strUserComment = request.getParameter("comment");
String rtitle = "";
String server = "127.0.0.1";
String sql = "insert into Rcomputer values(?,?)";
boolean flag = true;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pst = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:oracle:thin:@"+server+":1521:CONG";
conn=DriverManager.getConnection(url,"system","system");
stmt=conn.createStatement();
rs = stmt.executeQuery("select rtitle from Rcomputer"); pst = conn.prepareStatement(sql);
pst.setString(1,strUserRtitle);
pst.setString(2,strUserComment);
pst.executeUpdate();
out.println("<a href='computer_display.jsp'>评论成功</a>");
}catch(SQLException e){
out.println(e.getMessage());
}
stmt.close();
conn.close();
%>
实验证明已经写进去了,但是当我第二遍再重新读数据库里面时,却怎么也读不了了,而且不能正常显示页面,只有把表删了重建才能成功显示,我实在是没办法了,请不吝赐教,谢谢!
(只要能解决,我一定给最高分)
create table computer(
title varchar2(30),
matter varchar2(2000)
);
create table Rcomputer(
rtitle varchar(30),
rmatter varchar(2000)
);
这是我第一个文件从数据库里读两个表的代码,
<%
request.setCharacterEncoding("gb2312");
String strUsertitle=request.getParameter("title");
String matter="";
String server="127.0.0.1";
String TITLE="";
String reply="";
String rtitle="";
String rmatter="";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ResultSet rs1 = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:oracle:thin:@"+server+":1521:CONG";
conn=DriverManager.getConnection(url,"system","system");
stmt=conn.createStatement();
rs = stmt.executeQuery("select title,matter from computer");
while(rs.next())
{
TITLE=rs.getString("title");
if(TITLE.equals(strUsertitle))
{
matter=rs.getString("matter");
out.println("<font color = 'Blue'>"+"标题:"+TITLE+"<br>");
out.println(matter+"<br>");
break;
}
}
}catch(SQLException e){
out.println(e.getMessage());
}
rs1 = stmt.executeQuery("select rtitle,rmatter from Rcomputer");
while(rs1.next())
{
rtitle=rs1.getString(rtitle);
if(rtitle.equals(strUsertitle))
{
reply="回复:"+rtitle;
out.println("标题:"+rtitle+"<br>");
out.println(rmatter+"<br>");
}
}
stmt.close();
conn.close();
%>
这是我的另一个王Rcomputer里写东西的代码:
<%
request.setCharacterEncoding("gb2312");
String strUserRtitle = request.getParameter("rtitle");
String strUserComment = request.getParameter("comment");
String rtitle = "";
String server = "127.0.0.1";
String sql = "insert into Rcomputer values(?,?)";
boolean flag = true;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pst = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:oracle:thin:@"+server+":1521:CONG";
conn=DriverManager.getConnection(url,"system","system");
stmt=conn.createStatement();
rs = stmt.executeQuery("select rtitle from Rcomputer"); pst = conn.prepareStatement(sql);
pst.setString(1,strUserRtitle);
pst.setString(2,strUserComment);
pst.executeUpdate();
out.println("<a href='computer_display.jsp'>评论成功</a>");
}catch(SQLException e){
out.println(e.getMessage());
}
stmt.close();
conn.close();
%>
实验证明已经写进去了,但是当我第二遍再重新读数据库里面时,却怎么也读不了了,而且不能正常显示页面,只有把表删了重建才能成功显示,我实在是没办法了,请不吝赐教,谢谢!
(只要能解决,我一定给最高分)