java以输入形式(JSP)显示数据库中的值
我在寻找更好的解决方案。我正在为我的学校项目制作一个匹配的文字游戏,但我有一些问题。我不希望使用这种代码样式,而是希望从sql数据库中随机显示我的值,但不要重复相同的单词。我尝试了一些东西,但它不起作用,所以我将感谢你的任何帮助
这是我的litlle游戏的截图:when i click start words will be open, and the game will start...
我的代码是:
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%! String[] wordsleft ={"Belgrade","Zagreb","Sarajevo", "Washington", "Paris" };
%>
<%! String[] wordsright ={"Serbia","Bosnia","Croatia","France","USA"};
%>
<%!
String printleft(){
Random rand = new Random();
int a = rand.nextInt(4 - 0 + 1) + 0;
return String.valueOf(wordsleft[a]);
} %>
<%!
String printright(){
Random rand = new Random();
int a = rand.nextInt(4 - 0 + 1) + 0;
return String.valueOf(wordsright[a]);
} %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="cssstyle.css" rel="stylesheet">
<title>Matching Words</title>
</head>
<body>
<h1 id="welcome">Welcome!</h1>
<p class="tekst">Spojnica je igra sa rečima. <br><br>U dve kolone je prikazano po 5 reči.
Svaka reč u levoj koloni ima svoj par u desnoj koloni. Za svaki spojen par dobijate 3 poena.
</p>
<div id="container">
<div class="containerlevo">
<input type="submit" class="leftword" name="prvo" id="firstleft" value="?" />
<input type="submit" class="leftword" name="drugo" id="secondleft" value="?" />
<input type="submit" class="leftword" name="trece" id="thirdleft" value="?" />
<input type="submit" class="leftword" name="trece" id="fourthleft" value="?" />
<input type="submit" class="leftword" name="trece" id="fifthleft" value="?" />
</div>
<div class="containerdesno">
<input type="submit" class="rightword" name="prvo" id="firstright" value="?" />
<input type="submit" class="rightword" name="drugo" id="secondright" value="?" />
<input type="submit" class="rightword" name="trece" id="thirdright" value="?" />
<input type="submit" class="rightword" name="drugo" id="fourthright" value="?" />
<input type="submit" class="rightword" name="trece" id="fifthright" value="?" />
</div>
<form action="Rezultat" method="post" >
<input class="button start" type="button" id="start" onclick = "printright(); printleft();" value="START" name="start"/>
<input class="button" type="submit" id="end" value="END" name="end"/>
</form>
</div>
</body>
</html>
<script>
function printleft(){
document.getElementById("firstleft").value = "<%= printleft() %>";
document.getElementById("secondleft").value = "<%= printleft() %>";
document.getElementById("thirdleft").value = "<%= printleft() %>";
document.getElementById("fourthleft").value = "<%= printleft() %>";
document.getElementById("fifthleft").value = "<%= printleft() %>";
document.getElementById("start").disabled = true;
}
function printright(){
document.getElementById("firstright").value = "<%= printright() %>";
document.getElementById("secondright").value = "<%= printright() %>";
document.getElementById("thirdright").value = "<%= printright() %>";
document.getElementById("fourthright").value = "<%= printright() %>";
document.getElementById("fifthright").value = "<%= printright() %>";
document.getElementById("start").disabled = true;
}
# 1 楼答案
在姓名上使用数据库有很多东西需要添加和学习。也许你想从名字文件开始——这似乎更容易开始
尽管如此。首先需要创建一个数据库,根据使用的jdbc驱动程序,您将创建到数据库的jdbc连接字符串(这里是^{)
然后,您需要在应用程序中加载驱动程序类一次,通常是在
init()
或者更好地作为上下文侦听器。在使用JDBC时,最好在应用程序部署描述符中定义一个数据源描述这方面的所有细节显然超出了本文的范围,而且肯定在其他地方写得更好。你可以找到很多例子:Netbeans有一个教程在这里展示https://netbeans.org/kb/docs/web/mysql-webapp.html
然后你应该从一个提供单词列表的类开始,一个模型类。model类将隐藏从JSP页面获取的所有数据
这就是你应该做的:从JSP中删除所有函数,并将数据处理放在一个模型类中。然后在JSP中使用该模型类来提供数组
当你这样做的时候,你只需要把模型改为从文件中读取,而不是常量数组。如果你有工作,处理数据库