您的位置:首页 > 数据库 > MySQL

【JSP+Ajax+MySQL】使用JSP连接MySQL数据库

2014-11-30 13:17 591 查看
1、在MyEclisep中新建一个Web工程。

2、下载mysql-connector-java-5.1.22-bin.jar,地址:http://download.csdn.net/detail/joanna_yan/8209703

把mysql-connector-java-5.1.22-bin.jar放到工程的lib下



3、index.jsp如下,在同一个页面使用多个Ajax任务。

<!DOCTYPE html>
<html>
<head>
<script>
var xmlhttp;
function loadXMLDoc(url,url2,cfunc)
{
if (window.XMLHttpRequest)
{// IE7+, Firefox, Chrome, Opera, Safari 代码
xmlhttp=new XMLHttpRequest();
xmlhttp2=new XMLHttpRequest();
}
else
{// IE6, IE5 代码
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
xmlhttp2.onreadystatechange=cfunc;
xmlhttp2.open("GET",url2,true);
xmlhttp2.send();
}

function myFunction()
{

loadXMLDoc("1.jsp","2.jsp",function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;
document.getElementById("myDiv2").innerHTML=xmlhttp2.responseText;
}
});

}
</script>
</head>
<body>

<div id="myDiv1"><h2>Book'price is 45</h2></div>
<div id="myDiv2"><h2>Book'price is 49</h2></div>
<button type="button" onclick="myFunction()">Search</button>

</body>
</html>


1.jsp如下

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<html>
<body>

<%
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/books","root","123456");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from book where price=45");
while(rst.next())
{
out.println(rst.getString("bookName"));
}
//关闭连接、释放资源
rst.close();
stmt.close();
con.close();
%>

</body>
</html>


2.jsp如下

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<html>
<body>

<%
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/books","root","123456");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from book where price=49");
while(rst.next())
{
out.println(rst.getString("bookName"));
}
//关闭连接、释放资源
rst.close();
stmt.close();
con.close();
%>

</body>
</html>


数据库如下



CREATE TABLE `book` (
`bookId` varchar(50) NOT NULL DEFAULT '',
`bookName` varchar(50) DEFAULT NULL,
`publisher` varchar(100) DEFAULT NULL,
`price` float DEFAULT NULL,
PRIMARY KEY (`bookId`)
)
INSERT INTO `book` VALUES ('1001', 'Tomcat与Java Web开发技术详解', '电子工业出版社', '45');
INSERT INTO `book` VALUES ('1002', '精通Struts:基于MVC的Java Web设计与开发', '电子工业出版社', '49');
INSERT INTO `book` VALUES ('1003', '精通Hibernater:Java对象持久化技术详解', '电子工业出版社', '59');
INSERT INTO `book` VALUES ('1004', '精通EJB', '电子工业出版社', '89');
INSERT INTO `book` VALUES ('1005', 'J2EE应用与BEA Weblogic Server', '电子工业出版社', '56');
INSERT INTO `book` VALUES ('1006', 'JSP', '电子工业出版社', '23');


4、运行结果



点Search之后



从数据库读取了价格为45和49元的书名。

本工程实例下载地址:http://download.csdn.net/detail/joanna_yan/8209767
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: