搜索热词
UploadNews.jsp
pageEncoding="utf-8"%>
上传新闻function upload(){
document.getElementById("article").value = document
.getElementById("content").value;
document.getElementById("formid").submit();
}
title name="title" value="a" size="40"> |
author size="40"> |
name="articleName" /> |
date(xxxx.xx.xx) |
换行函数在ShangchuanNews.jsp 代码如下
pageEncoding="utf-8" %>
上传新闻public String turn(String str) {
while (str.indexOf("\n") != -1) {
str = str.substring(0,str.indexOf("\n")) + "
"
+ str.substring(str.indexOf("\n") + 1);
}
while (str.indexOf(" ") != -1) {
str = str.substring(0,str.indexOf(" ")) + " "
+ str.substring(str.indexOf(" ") + 1);
}
return str;
}
%>
try {
request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
String author = request.getParameter("author");
String article = request.getParameter("articleName");
String articlebr = turn(article);
String date = request.getParameter("date");
String driverClass="com.MysqL.jdbc.Driver";
String url = "jdbc:MysqL://****.****/****?characterEncoding=utf8";//存到数据库不会乱码
String user="***";
String password="****";
Connection conn;
int i=0;
Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url,user,password);
String sql = "insert into news (id,title,author,article,date) "
+ "values(?,?,?)";
Connection conn1 = DriverManager.getConnection(url,password);
PreparedStatement pstmt;
pstmt = (PreparedStatement) conn1.prepareStatement(sql);
pstmt.setString(1,null);
pstmt.setString(2,title);
pstmt.setString(3,author);
pstmt.setString(4,articlebr);
pstmt.setString(5,date);
i = pstmt.executeUpdate();
conn1.close();
pstmt.close();
out.println("
上传成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
以上这篇jsp实现textarea中的文字保存换行空格存到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。