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;
}
}
展开
 我来答
殒尘
2011-03-30 · TA获得超过473个赞
知道小有建树答主
回答量:351
采纳率:0%
帮助的人:368万
展开全部
这个方法,addMessage(),第一行sql语句,使用?作为占位符
String reg = "insert into message_info values(?,?,?,?,?)";
追问
oracle 是不应该用 : 冒号作为站位符吗 ?
追答
jdbc不管,它是通用的,这缓存是jdbc实现的,又不是oracle,所以jdbc说了算
freejsdou
2011-03-30 · TA获得超过421个赞
知道小有建树答主
回答量:364
采纳率:100%
帮助的人:377万
展开全部
String reg = "insert into message_info values(:,:,:,:,:)";
是不是改成
String reg = "insert into message_info(name,title 等等字段对应上。 ) values(:,:,:,:,:)";
另外你最后
(:,:,:,:,:)是不是改成(?,?,?,?,?) ?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式