JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區 » MySQL  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 mysql資料表的對應
蔚藍





發文: 14
積分: 0
於 2011-11-24 00:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟做了一個簡單JSP的會員登入系統
第一個資料表放的是:MemberID,Account,Password
第二個資料表:ScoreID,Gamescore,Account
用Navicat 對分數資料表的Accout和會員的帳號作外部索引鍵
但做完之後,原本FLASH正常可以傳分數給資料庫
卻出現傳不了的情形,在去看看JSP的錯誤如下
Cannot add or update a child row: a foreign key constraint fails

----------------------------------------------------------------------
換了另一個方法,一開始會員登入時,我有存到session裡
想說把他跟分數一起傳給資料庫
但是String a=<%=session.getAttribute("account")%>;
也錯誤@@
只能直接<%=session.getAttribute("account")%>才有東西
那我又想說直接寫在SQL裡
VALUES('"+gamescore+"','"+<%=session.getAttribute("account")%>+"')" );
也出錯
到底該怎麼做才能做好對應呢~"~??


reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
ynchang





發文: 151
積分: 0
於 2011-11-24 09:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請先檢查看看你產生出的Insert語法跟你想要的東西是否吻合

reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
limayke





發文: 23
積分: 0
於 2011-11-24 11:30 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
session.getAttribute("account")

這句出來應該是物件

你應該要轉成字串才能加進sql

String.valueOf(session.getAttribute("account"))


reply to postreply to post
作者 Re:mysql資料表的對應 [Re:limayke]
蔚藍





發文: 14
積分: 0
於 2011-11-24 12:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思= =|

我把它改成下面這樣
String account=session.getAttribute("account").toString();

tomcat的錯誤訊息出現值是NULL耶java.lang.NullPointerException
可是在其他的JSP上接值,卻是正確的帳號

不知道怎麼回事= =|

還是錯在String account=session.getAttribute("account").toString();這行...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String gamescore=request.getParameter("gamescore");
String account = session.getAttribute("account").toString();
Driver div=(Driver)Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=big5","root", "12345");
PreparedStatement chaxun_ps = conn.prepareStatement("SELECT * FROM score WHERE gamescore = '" + gamescore+ "'");
ResultSet chaxun_rs = chaxun_ps.executeQuery();
boolean chaxun_success = chaxun_rs.next();
if(chaxun_success){
out.print("b=3"); 
}else{
PreparedStatement register_ps = conn.prepareStatement("INSERT INTO score (account,gamescore) VALUES('"+account+"','"+gamescore+"')" );
out.clear();
out.print("b="+register_ps.executeUpdate()+"");
}
chaxun_rs.close();
chaxun_ps.close();
conn.close();
%>


蔚藍 edited on 2011-11-24 12:53
reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
蔚藍





發文: 14
積分: 0
於 2011-11-24 13:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
把它改成String account = (String)session.getAttribute("account");
又有值了= =|
錯誤在地24行?
23: out.clear();
24: out.print("b="+register_ps.executeUpdate()+"");
錯誤訊息
javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`score`, CONSTRAINT `aa` FOREIGN KEY (`account`) REFERENCES `list` (`account`))

把ACCOUNT寫死可以傳進去,但為何這樣子傳不進去呢~"~


蔚藍 edited on 2011-11-24 14:09
reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
ynchang





發文: 151
積分: 0
於 2011-11-24 16:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問list這個table,account欄位中有你輸入的值嗎???

reply to postreply to post
作者 Re:mysql資料表的對應 [Re:ynchang]
蔚藍





發文: 14
積分: 0
於 2011-11-24 16:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有阿有阿~"~

reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
ynchang





發文: 151
積分: 0
於 2011-11-24 17:23 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
那你可能要看看你的Table engine是甚麼了...
可以使用
1
2
select table_name,engine for information_schema.tables
where table_name = ???


目前Foreign key只有InnoDB支援
MyISAM是不支援的

可參考:
http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html
http://dev.mysql.com/doc/refman/5.6/en/myisam-storage-engine.html


reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
limayke





發文: 23
積分: 0
於 2011-11-25 11:22 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
String.valueOf(session.getAttribute("account"))

我寫的是這樣吧..

為什麼不是寫session.getAttribute("account").toString();

或者(String)session.getAttribute("account")

因為上面那兩種寫法我杯具太多次了


reply to postreply to post
作者 Re:mysql資料表的對應 [Re:limayke]
蔚藍





發文: 14
積分: 0
於 2011-11-25 15:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
資料表的型態是InnoDB沒錯@@"

limayke大的方法,我輸出的值也是null耶= =|
到底是為何呢
但在別的JSP上接到的都是正確的值阿~"~


蔚藍 edited on 2011-11-25 15:43
reply to postreply to post
作者 Re:mysql資料表的對應 [Re:蔚藍]
蔚藍





發文: 14
積分: 0
於 2011-11-27 14:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝各位大大的幫忙...
小弟找到錯誤的地方了
我傳回資料庫的JSP跟原本登入頁面的JSP放的是不同的資料夾@@
也就當然抓不到SESSION~~
一切都是美麗的誤會哈哈
因為上述那3種都可以傳回資料庫XD
小弟真的是太粗心了~"~


reply to postreply to post
» JWorld@TW »  JDBC/SQL討論區 » MySQL

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8