java.sql.SQLException: 无效的列索引 做练习的时候遇到个问题,一直没解决,希望高手指点一下。谢谢。
MessageBean代码:packagecom.jspdev.ch5;importjava.sql.*;importjava.util.*;importjava.sql...
MessageBean代码:
package com.jspdev.ch5;
import java.sql.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class MessageBean {
private Connection con;
MessageVO msg;
// 获得数据库连接。
public MessageBean() {
String CLASSFORNAME = "oracle.jdbc.driver.OracleDriver";
String SERVANDDB = "jdbc:oracle:thin:@10.4.129.114:1521:VUSPDB";
String USER = "giskf";
String PWD = "vusp";
try {
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB, USER, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setMessage(MessageVO msg) {
this.msg = msg;
}
public void addMessage() {
String reg = "insert into message_info values(:,:,:,:,:)";
try {
PreparedStatement pstmt = con.prepareStatement(reg);
pstmt.setString(1, msg.getName());
pstmt.setString(2, msg.getTitle());
/*
* if ((msg.getEmail()).length() == 0) stm.setString(3, null); else
*/
pstmt.setString(3, msg.getEmail());
pstmt.setString(4, msg.getContent());
pstmt.setDate(5, new java.sql.Date(new java.util.Date().getTime()));
pstmt.executeQuery();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Collection getMessage() throws Exception {
Collection ret = new ArrayList();
try {
Statement stm = con.createStatement();
ResultSet result = stm
.executeQuery("select count(*) from MESSAGE_INFO");
int message_count = 0;
if (result.next()) {
message_count = result.getInt(1);
result.close();
}
if (message_count > 0) {
result = stm
.executeQuery("select * from MESSAGE_INFO order by time desc");
while (result.next()) {
String name = result.getString("names");
String title = result.getString("title");
String mail = result.getString("mail");
String content = result.getString("content");
java.sql.Date date = result.getDate("dates");
MessageVO message = new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(date);
message.setEmail(mail);
ret.add(message);
}
result.close();
stm.close();
}
con.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return ret;
}
}
MessageVO 是一个属性javabean。
addMessage.jsp关键代码:
<%@ page import="java.sql.*,com.jspdev.ch5.*,java.util.*,java.sql.*"
errorPage="error.jsp"%>
<jsp:useBean id="messageVo" class="com.jspdev.ch5.MessageVO"
scope="page">
<jsp:setProperty name="messageVo" property="*" />
</jsp:useBean>
<jsp:useBean id="messageBean" class="com.jspdev.ch5.MessageBean"
scope="page" />
<HTML>
<BODY>
<%
messageBean.setMessage(messageVo);
messageBean.addMessage();
%>
<</body>
</html>解决问题了再给 15分。
MessageVO代码:
package com.jspdev.ch5;
public class MessageVO implements java.io.Serializable {
private String name, email, title, content;
private java.sql.Date date;
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public String getName() {
return this.name;
}
public String getContent() {
return this.content;
}
public String getTitle() {
return this.title;
}
public String getEmail() {
return this.email;
}
public java.sql.Date getDate() {
return this.date;
}
public void setDate(java.sql.Date date) {
this.date = date;
}
} 展开
package com.jspdev.ch5;
import java.sql.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class MessageBean {
private Connection con;
MessageVO msg;
// 获得数据库连接。
public MessageBean() {
String CLASSFORNAME = "oracle.jdbc.driver.OracleDriver";
String SERVANDDB = "jdbc:oracle:thin:@10.4.129.114:1521:VUSPDB";
String USER = "giskf";
String PWD = "vusp";
try {
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB, USER, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setMessage(MessageVO msg) {
this.msg = msg;
}
public void addMessage() {
String reg = "insert into message_info values(:,:,:,:,:)";
try {
PreparedStatement pstmt = con.prepareStatement(reg);
pstmt.setString(1, msg.getName());
pstmt.setString(2, msg.getTitle());
/*
* if ((msg.getEmail()).length() == 0) stm.setString(3, null); else
*/
pstmt.setString(3, msg.getEmail());
pstmt.setString(4, msg.getContent());
pstmt.setDate(5, new java.sql.Date(new java.util.Date().getTime()));
pstmt.executeQuery();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Collection getMessage() throws Exception {
Collection ret = new ArrayList();
try {
Statement stm = con.createStatement();
ResultSet result = stm
.executeQuery("select count(*) from MESSAGE_INFO");
int message_count = 0;
if (result.next()) {
message_count = result.getInt(1);
result.close();
}
if (message_count > 0) {
result = stm
.executeQuery("select * from MESSAGE_INFO order by time desc");
while (result.next()) {
String name = result.getString("names");
String title = result.getString("title");
String mail = result.getString("mail");
String content = result.getString("content");
java.sql.Date date = result.getDate("dates");
MessageVO message = new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(date);
message.setEmail(mail);
ret.add(message);
}
result.close();
stm.close();
}
con.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return ret;
}
}
MessageVO 是一个属性javabean。
addMessage.jsp关键代码:
<%@ page import="java.sql.*,com.jspdev.ch5.*,java.util.*,java.sql.*"
errorPage="error.jsp"%>
<jsp:useBean id="messageVo" class="com.jspdev.ch5.MessageVO"
scope="page">
<jsp:setProperty name="messageVo" property="*" />
</jsp:useBean>
<jsp:useBean id="messageBean" class="com.jspdev.ch5.MessageBean"
scope="page" />
<HTML>
<BODY>
<%
messageBean.setMessage(messageVo);
messageBean.addMessage();
%>
<</body>
</html>解决问题了再给 15分。
MessageVO代码:
package com.jspdev.ch5;
public class MessageVO implements java.io.Serializable {
private String name, email, title, content;
private java.sql.Date date;
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public String getName() {
return this.name;
}
public String getContent() {
return this.content;
}
public String getTitle() {
return this.title;
}
public String getEmail() {
return this.email;
}
public java.sql.Date getDate() {
return this.date;
}
public void setDate(java.sql.Date date) {
this.date = date;
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询