iT邦幫忙

0

java insert mysql 沒反應

大家好:
想請教,我網頁填寫的資料
要insert到資料庫,但一直失敗
找了很久不知錯在哪...

n2.jsp

<body>
<div class="container">
    <form class="form-inline">
     zx01: <input type="name" class="form-control" id="zx01"  name="zx01">   
   zx02: <input type="company" class="form-control" id="zx02"  name="zx02">
   zx03:<input type="name" class="form-control" id="zx03"  name="zx03">     
   <button submit="submit">submit</button>
  </form> 
</div>
</body>

<%   
    Basic b=new Basic();       
 String zx01 =request.getParameter("zx01");
   String zx02=request.getParameter("zx02"); 
   String zx03=request.getParameter("zx03");
   out.print(zx01);
    List<zx> z=b.Getzx2(zx01,zx02,zx03); 
%>

以下是basic.java 的程式
去查了hibernate的文件照著打,但他就是無法insert....

  public List Getzx2(String zx01,String zx02,String zx03)
    { 
	  Query query= session.createQuery("insert into zx(zx01,zx02,zx03)" +
    			"select zx01,zx02,zx03 from zx");
                // query.setString(zx01, zx02,zx03);
	   int result = query.executeUpdate();
        return query.list();
    }

參考網站
https://howtodoinjava.com/hibernate/complete-hibernate-query-language-hql-tutorial/#insert_operatio

謝謝大家

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
暐翰
iT邦大師 1 級 ‧ 2018-07-16 23:45:02
最佳解答

這邊有兩個觀念

  1. createQuery使用HQL不是SQL
  2. Hibernate強調的是物件化概念

瞭解上面兩個概念後,下面是問題點跟解決方式:

  • HQL-createQuery 只支援insert select
    看你的邏輯不需要這個的。
    請使用createSQLQuery
    或是創建持久化實體來新增
    (官方也建議使用,但前提是你要映射好類別)

舉例:

MyEntity e=new MyEntity();
e.setXXXX(XXX,values);
save(e);
  • query.setString(zx01, zx02,zx03);
    setString不是這樣使用
    它需要對應查詢字串裡面的:參數名稱
    使用setString(參數名稱,)方式傳值給DBMS

使用createSQLQuery demo:

	public boolean insertZx(String zx01,String zx02,String zx03)
	{ 

		Query query= session.createSQLQuery("insert into zx(zx01,zx02,zx03) values (:zx01,:zx02,:zx03)");
		query.setString("zx01", zx01);
		query.setString("zx02", zx02);
		query.setString("zx03", zx03);
		try {
			query.executeUpdate();
		} catch (Exception e) {
			//簡單捕捉錯誤
			e.printStackTrace();
			return false;
		}		
		return true;//新增成功
	}
看更多先前的回應...收起先前的回應...
神威 iT邦研究生 4 級 ‧ 2018-07-17 09:41:59 檢舉

你好:
我java那邊照著你的方式打,但是
我前端這邊輸入訊息還是沒反應說.....

<%   Basic b=new Basic();
	    if(request.getParameter("insert") != null){ 
	        String zx01 = request.getParameter("zx01"); 
   String zx02 = request.getParameter("zx02"); 
    String zx03 = request.getParameter("zx03"); 
	     b.insertZx(zx01, zx02, zx03);
	    } 
	%> 
<html>  
<body> 
<form action = "n3.jsp" method = "post"> 
z1 : <input type = "text" name = "zx01" size = "10"> 
z2: <input type = "text" name = "zx02" size = "10"> 
z3: <input type = "text" name = "zx03" size = "10"> 
<input type = "submit" name = "insert" value = "Insert"> 
</form> 
 </body> 
 </html>

我輸入資料,按insert的按鍵
但是我的mysql都沒這筆資料....

暐翰 iT邦大師 1 級 ‧ 2018-07-17 09:57:44 檢舉
  1. b.insertZx返回值是true還是false?
  2. 使用debug確定n3.jsp有去呼叫insertZx

先列印出request是不是有取到值~
再看SQL問題@@...

神威 iT邦研究生 4 級 ‧ 2018-07-17 11:18:50 檢舉

純真的人
我有取到zx01,02,03的值
暐翰
用了debug
發現n3.jsp沒有呼叫到insertZx....
https://ithelp.ithome.com.tw/upload/images/20180717/20102983YL0hsiRbYN.jpg

暐翰 iT邦大師 1 級 ‧ 2018-07-17 11:33:35 檢舉

確定呼叫網址跟JSP位置是否正確

神威 iT邦研究生 4 級 ‧ 2018-07-17 11:49:16 檢舉

有,路徑都正確的

神威 iT邦研究生 4 級 ‧ 2018-07-17 14:19:17 檢舉

我直接用jsp連mysql
不透過java就OK了....
不過發現打中文的時候,顯示就會變亂馬耶
我jsp是用UTF-8

<%@page contentType="text/html" pageEncoding="UTF-8"%>

mysql
用的是 utf8_general_ci(因為找不到單純的UTF-8...
想問要怎樣轉才不亂碼

暐翰 iT邦大師 1 級 ‧ 2018-07-17 14:31:43 檢舉

中文亂碼是另外問題了
請看這篇
解决JSP中文乱码问题 - 好久不贱 - 博客园

神威 iT邦研究生 4 級 ‧ 2018-07-17 15:06:14 檢舉

你好:
其實我爬過好幾篇文了
試過加入

<%@ page contentType="text/html; charset=gb2312"%>
或
<%@ page contentType="text/html; charset=UTF-8"%>

或者是在
web.xml開頭加入

<?xml version="1.0" encoding="UTF-8"?>

或是在tomcat中加入

<Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="UTF-8" />

但是都沒用....
&
我加入這行指令時他會顯示錯誤
https://ithelp.ithome.com.tw/upload/images/20180717/20102983BHoOTIYvtH.png

暐翰 iT邦大師 1 級 ‧ 2018-07-17 15:53:35 檢舉

1.JSP宣告(放這個在最前面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

2.html增加這段

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

cool3690

神威 iT邦研究生 4 級 ‧ 2018-07-17 16:17:04 檢舉

你好:
還是有問題耶,如圖
https://ithelp.ithome.com.tw/upload/images/20180717/20102983XUNgYhXVFh.png
以下是我的程式....

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file = "db.jsp"%> //這是mysql 連線

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<html xmlns="http://www.w3.org/1999/xhtml">
 <body> 
 <% 
 if(request.getParameter("insert") != null){ 
	        String z1 = request.getParameter("z1"); 
String sql = "insert into tt (z1) values ('" + z1 + "')";         int a = stmt.executeUpdate(sql);   
	        } 
	 String sql = "select * from tt"; 
	 ResultSet rs = stmt.executeQuery(sql); 
	 while(rs.next()){ 
  String name = rs.getString("z1"); 	                   
	out.print("Name : " + name +"<br>"); 
	                } 
	                rs.close(); 
	        %> 
	<form action = "n4.jsp" method = "post"> 
 Name : <input type = "text" name = "z1" size = "10"> 
<input type = "submit" name = "insert" value = "Insert"> 
</form> 
</body> 
</html> 

他還是一直亂碼阿~~~

暐翰 iT邦大師 1 級 ‧ 2018-07-17 17:47:55 檢舉

reqeust的字串接收格式ISO-8859-1轉UTF-8

request.setCharacterEncoding("UTF-8");
String z1 =  new String((request.getParameter("z1")).getBytes("ISO-8859-1"),"UTF-8");
神威 iT邦研究生 4 級 ‧ 2018-07-17 18:00:46 檢舉

https://ithelp.ithome.com.tw/upload/images/20180717/20102983LLBmykCTI9.jpg
這行放下去一直錯誤阿....

暐翰 iT邦大師 1 級 ‧ 2018-07-17 23:54:03 檢舉

sorry 少打一個)
並且看到是post請求,只要改這樣就可以

request.setCharacterEncoding("UTF-8");

先說這是最不好的處理方式

因為編碼可以在伺服器設定那邊調整支持UTF-8
但是我不知道你使用server是甚麼 (tomcat?)
所以用土法煉鋼方式

神威 iT邦研究生 4 級 ‧ 2018-07-18 08:23:16 檢舉

你好:
我用的是netbean平台tomcat server
&
少打一個括號昨天我有發現並補上喔
但還是錯誤...

<% 
 if(request.getParameter("insert") != null){ 
request.setCharacterEncoding("UTF-8");
   String z1 =  new String(request.getParameter("z1").getBytes("ISO-8859-1","UTF-8"));
String sql = "insert into tt (z1) values ('" + z1 + "')"; 
 int a = stmt.executeUpdate(sql);
	        } 
 rs.close(); 
 %> 
<form action = "n4.jsp" method = "post"> 
 Name : <input type = "text" name = "z1" size = "10"> 
 <input type = "submit" name = "insert" value = "Insert"> 
 </form> 
神威 iT邦研究生 4 級 ‧ 2018-07-18 13:24:11 檢舉

謝謝大大

0
純真的人
iT邦大師 1 級 ‧ 2018-07-16 16:11:30

雖然沒寫過java
但看你的程式碼..
沒有串到..

至少應該要這樣吧

public List Getzx2(String zx01,String zx02,String zx03)
    { 
	  Query query= session.createQuery("insert into zx(zx01,zx02,zx03)" +
    			"select '" + zx01 + "' as zx01,'" + zx02 + "' as zx02,'" + zx03 + "' as zx03 ");
                
	   int result = query.executeUpdate();
        return query.list();
    }
神威 iT邦研究生 4 級 ‧ 2018-07-16 16:36:54 檢舉

我當初是這樣打的

"insert into zx (zx01,zx02,zx03) values ('" + zx01 + "','" + zx02 + "','" + zx03 + "')"

但都沒反應,看到其他人這麼打,才想說改來試試看

但你那句是錯的@@

insert into zx(zx01,zx02,zx03) 
select zx01,zx02,zx03 from zx

沒有查詢自己又新增自己~

0
Luke
iT邦研究生 5 級 ‧ 2018-07-16 16:41:24

Hibernate 不是用一個 Object(zx)
當 Object(zx) SET 資料
然後 就會自己INSERT 到資料了不是嗎?/images/emoticon/emoticon06.gif

還有您沒有提供錯誤訊息,也不知道
HQL Tutorial 是設定錯誤還是語法錯誤?

看來應該是您的 SQL 有問題
如 純真的人 說的

要INSERT 資料,您要去 查select zx01,zx02,zx03 from zx
這樣不是全查嗎?沒有條件?
若是DB 沒有資料,就會永遠都不會INSERT
若是DB 有資料,就會倍增增加,2次方倍增

我要發表回答

立即登入回答