请求各位大老帮忙!!!!!!!!

sundeythetop 2012-05-16 03:26:04
我是刚进公司现在学习中。
现在要做一个bbs
就跟我这个发表新话题一样的感觉。
要把文件名,内容,名字通过jsp,java,sql。
把输入的东西变成一行存到数据库里,然后再显示到网页上。
我现在数据进去了,但是一列。要怎么变成一行啊?
小弟求教!!!!!
...全文
123 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
d945332077 2012-05-16
  • 打赏
  • 举报
回复
数据库 一列 一行??有点蒙了 你存入的是一个对象 对象的属性在数据库中就是一样 存入一列 ??这个有点不明白了 蛋疼
sundeythetop 2012-05-16
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.ArrayList"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
ArrayList<String> list = (ArrayList<String>)session.getAttribute("list");
String errorMessage = session.getAttribute("errorMessage").toString();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bullletin board system</title>
</head>
<body>

<center><h1>Bulletin Board System</h1>
BBS FORM
<form name = "form" method = "post" action = "/ServletSample/sample">
登録者名
<input type = "text" name = "name" size ="30" maxlength="20"><br><br>
件名
<input type = "text" name = "title" size ="30" maxlength="20"><br><br>
メッセージ
<input type="text" name = "message" size ="60" maxlength="250" style=" height: 8em; padding-top:2px"><br><br>
パスワード
<input type = "password" name = "password" size ="8" maxlength="4"><br><br>
<input type = "submit" name = "Regist" value = "作成"><br><br>
タイトル<br><br>

<%
if(errorMessage != null && !errorMessage.equals("")){
%>
<font color = "red">
<%=errorMessage %>
</font>
<%
}else if(list != null && list.size() !=0){
%>
<table border = "2">
<%
for(int i = 0; i < list.size() ;i++){
%>
<tr>
<td><%=list.get(i) %>
</td>
<td><input type = "submit" name = "delete" value = "削除">
</td>
</tr>
<%
}
%>
</table>
<%
}
%>
</form>
</center>
</body>
</html>


现在是能让数据现实出来了。
但是显示的数据下面要有删除的按钮。
我添加了删除按钮但是是连续出来三个啊。
要怎么才能出来一个按钮呢?
zhangma83 2012-05-16
  • 打赏
  • 举报
回复
SampleClass.insertProcess(name);
SampleClass.insertProcess(title);
SampleClass.insertProcess(message);
SampleClass.insertProcess(password);
你上边这4句同等于执行了4句insert
insertProcess这个自定义函数参数肯定不是这样的,应该这几个你想写入的字段的一个拼接的字符串

在insertProcess这个自定义函数中有个ps.setString(1,str);方法。你看下他内部是怎么对你所传过来的字符串进行解析的。

错误的问题肯定是出在
SampleClass.insertProcess(name);
SampleClass.insertProcess(title);
SampleClass.insertProcess(message);
SampleClass.insertProcess(password);
这4句中,想办法吧4句变成1句吧,比如
string ss = name+";"+title+";"+message+";"+password;
SampleClass.insertProcess(ss);
只是打个比方,具体要看ps.setString(1,str);这个方法需要什么样的字符串进行解析
sundeythetop 2012-05-16
  • 打赏
  • 举报
回复
我还在学习阶段。也不懂什么新啊老啊~
老王@上新路 2012-05-16
  • 打赏
  • 举报
回复
你是在哪家公司,还用这么老的技术
sundeythetop 2012-05-16
  • 打赏
  • 举报
回复
还有一个文件
package sample;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class SampleServlet extends HttpServlet{
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

request.setCharacterEncoding("UTF-8"); // UTF-8でエンコードする
HttpSession session = request.getSession(); // セッションの取得

ArrayList<String> list = new ArrayList<String>(); // メッセージを格納する配列
String errorMessage = ""; // エラーメッセージを格納する変数

String regist = request.getParameter("Regist");

// セッションが新規か否か判定する
if(!session.isNew()){
if(regist != null && regist.length() != 0){
String name = request.getParameter("name"); // テキストのパラメータを取得
String title = request.getParameter("title");
String message = request.getParameter("message");
String password = request.getParameter("password");
// テキストが入力されているか否か
if(name != null && name.length() != 0
&& title != null && title.length() != 0
&& message !=null && message.length() != 0
&& password != null && password.length() == 3){
// 値をDBに登録
SampleClass.insertProcess(name);
SampleClass.insertProcess(title);
SampleClass.insertProcess(message);
SampleClass.insertProcess(password);
// DBに登録されている値を取得
list = SampleClass.selectProcess();
}else if(regist != null && regist.length() != 0){
errorMessage = "テキストを完成してください";
}
}
// セッションが新規の場合
}else{
list = SampleClass.selectProcess();
}
// セッションに値を格納
session.setAttribute("list",list);
session.setAttribute("errorMessage", errorMessage);
// 遷移先指定
request.getRequestDispatcher("Sample.jsp").forward(request, response);
}
}
sundeythetop 2012-05-16
  • 打赏
  • 举报
回复
这是jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.ArrayList"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
ArrayList<String> list = (ArrayList<String>)session.getAttribute("list");
String errorMessage = session.getAttribute("errorMessage").toString();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bullletin board system</title>
</head>
<body>

<center><h1>Bulletin Board System</h1>
BBS FORM
<form name = "form" method = "post" action = "/ServletSample/sample">
登録者名
<input type = "text" name = "name" size ="30" maxlength="20"><br><br>
件名
<input type = "text" name = "title" size ="30" maxlength="20"><br><br>
メッセージ
<input type="text" name = "message" size ="60" maxlength="250" style=" height: 8em; padding-top:2px"><br><br>
パスワード
<input type = "password" name = "password" size ="8" maxlength="4"><br><br>
<input type = "submit" name = "Regist" value = "作成">


<%
if(errorMessage != null && !errorMessage.equals("")){
%>
<font color = "red">
<%=errorMessage %>
</font>
<%
}else if(list != null && list.size() !=0){
%>
<table border = "2">
<%
for(int i = 0; i < list.size() ;i++){
%>
<tr><td><%=list.get(i) %></td></tr>
<%
}
%>
</table>
<%
}
%>
</form>
</center>
</body>
</html>
sundeythetop 2012-05-16
  • 打赏
  • 举报
回复
这是处理文件
package sample;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import sample.DBConnectClass;

public class SampleClass {
private static final String SELECT = "SELECT * FROM DISPLAY";
private static final String INSERT = "INSERT INTO DISPLAY VALUES(?)";

/**
* DBに格納されている値を取得し、ArrayListに格納し返す処理
* @return ArrayList<String>
*/
public static ArrayList<String> selectProcess(){
DBConnectClass dbConnect = new DBConnectClass();
ArrayList<String> list = new ArrayList<String>();

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;

try{
con = dbConnect.getConnection();
ps = con.prepareStatement(SELECT);

ps.executeQuery(); // SQL実行(select文)
rs = ps.getResultSet(); // 実行結果をResultSetに格納

// ResultSetに格納された値をBeanに格納する
while(rs.next()){
list.add(rs.getString("message"));
}

}catch(Exception e){
e.printStackTrace();
}finally{
//Connection,PreparedStatement,ResultSetの破棄
dbConnect.clear(con,ps,rs);
}
return list;
}
/**
*引数で受け取った文字列をDBに格納する処理
*@param str
*/
public static void insertProcess(String str){
DBConnectClass dbConnect = new DBConnectClass();

Connection con = null;
PreparedStatement ps = null;

try{
con = dbConnect.getConnection();
ps = con.prepareStatement(INSERT);

ps.setString(1,str);

ps.executeUpdate(); //SQL実行(Insert文)
con.commit(); //Commitをかける
}catch(Exception e){
e.printStackTrace();
try{
con.rollback();
}
catch(Exception ex){
ex.printStackTrace();
}
}finally{
dbConnect.clear(con,ps);
}
}
}
zhangma83 2012-05-16
  • 打赏
  • 举报
回复
SQL语句写错了呗~~~
一起混吧 2012-05-16
  • 打赏
  • 举报
回复
跟js有什么关系。你的java代码有问题呗。
zhangma83 2012-05-16
  • 打赏
  • 举报
回复
不明白
sundeythetop 2012-05-16
  • 打赏
  • 举报
回复
简单来说就是用java给数据库里插入一行数据!
我现在插入的都是一列数据!
001007009 2012-05-16
  • 打赏
  • 举报
回复
不懂楼主说的 一列变成一行 的意思

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧