2013年12月4日星期三

JSP form to submit data to the MySQL

my code inside the function is entered into add_text.jsp content submission , and then talk about the content of submitted written MySQL, ask Why is this you greatly to achieve operating [color = # FF0000] can be written only submit digital database , submitted in English and Chinese which can not be written to MySQL . SQL is defined inside two vachar type attribute , the encoding format is GBK. [/ color]

add_text.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>输入留言信息界面</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
    <script type="text/javascript"">
    function validate()
    {
    var Tname = document.forms[0].Tname.value;
    var Ttext = document.forms[0].Ttext.value;
     //document.getElementById("form").submit();
    }
    </script>
  </head>
  
  <body>
  <br>
  <center>
  <h2>添加留言文本</h2><hr>
 <form action="insert.jsp" method="post" id="form" onSubmit="return validate()" >
<h4>  昵称:<input type="text" name="Tname" class="{required:true}"></input><br></h4>
<h4>  留言内容:<input type="text" name="Ttext"></input><br></h4>
 <input type="submit" value="提交"/>
  </form>
  <a href="">查询所有留言</a>
  </center>
  </body>
</html>


insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>插入信息</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
    <body>
    <% 
    request.setCharacterEncoding("gbk");
    String Tname = request.getParameter("Tname");
    String Ttext = request.getParameter("Ttext");
    System.out.println(Ttext);
    Connection conn = null; 
    Statement stat = null; 
    ResultSet rs = null;
    Class.forName("com.mysql.jdbc.Driver"); 
    String url = "jdbc:mysql://localhost:3306/mldn"; 
    String user = "root"; 
    String password = "root";
    try{

conn = DriverManager.getConnection(url, user, password);

    stat = conn.createStatement(); 
    String sql = "insert into TextInfo(Tname,Ttext) values(" + Tname + ",'" + Ttext + "')";
    stat.executeUpdate(sql); 
    rs = stat.executeQuery("select * from student"); }

catch(Exception e){} 
%>
   
   <center>
   <%
    try{
    
    
    if(rs.next())
    {
    out.print("<br><h3>成功输入!</h3>");
    }
    else{
    out.print("<br><h3>输入失败!</h3>");
    }
  }
  catch(Exception e){}
    %>
  
   
      <br>
    <a href=add_text.jsp>返回添加信息页面</a>   <a href=showInfo.jsp>进入信息查询页面</a> 
    </center>
     <%
    if(rs != null)
    {
        rs.close();
        rs = null;
    }
        if(stat != null)
    {
        stat.close();
        stat = null;
    }
        if(conn != null)
    {
        conn.close();
        conn = null;
    }
    %>     
      </body>
</html>

------ Solution ------------------------------------- -------
As can be seen , you add a breakpoint to track and see where the problem in the end
------ Solution ---------- ----------------------------------
String sql = "insert into TextInfo (Tname, Ttext) values ​​("+ Tname +", '"+ Ttext +"') ";

this problem. Should be:
String sql = "insert into TextInfo (Tname, Ttext) values ​​('" + Tname + "', '" + Ttext + "')";
------ Solution -------------------------- ------------------

  
Thanks so much ! ! Really stupid mistake ah      
then ask how to solve the Chinese garbage problem?  
Try database using UTF-8
------ Solution ----------------------------- coding and coding database ---------------
ensure that the project is consistent on it.
------ For reference only -------------------------------------- -
Chinese submit to the background yet
------ For reference only ---------------------------- -----------


The crux of the problem is that I do not know why only submit figures to write to database tables, submitted in English and Chinese content inside the database table does not change before and submission .
------ For reference only -------------------------------------- -

hello , Eclispe inside the Console can see the data I enter , but there is no database table
------ For reference only --------- ------------------------------


just getting started with white developers still learning not quite know where to start would be the possibility of breakpoint debugging more intuitive help me analyze bug Thank
- ----- For reference only ---------------------------------------

just detailed testing under Tname this property only when the input digital content can be submitted to the database when
1. Tname when the content is digital , Ttext content in English can be submitted to the database and display the correct
2. Tname when the content is digital , Ttext content for the Chinese when the contents of the database garbled
3. Tname when a non- digital content are not committed to the database


------ For reference only ---------------------------------- -----


Thanks so much ! ! Really stupid mistake ah
then ask how to solve the Chinese garbage problem?
------ For reference only -------------------------------------- -

    
Thanks so much ! ! Really stupid mistake ah          
then ask how to solve the Chinese garbage problem?          
Try using UTF-8 database  

Well good , then if the database into UTF-8 is part of the JSP page encoding have changed , huh?

没有评论:

发表评论