1, 傳統的JDBC方法訪問數據庫
JSP代碼:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import ="java.sql.*,java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
try
{
out.println("Is going to create Database connecting");
out.println("<br>");
//Class.forName("com.mysql.jdbc.Driver");// 動態加載mysql驅動
//out.println("成功加載MySQL驅動程序");
// out.println("<br>");
//鏈接數據庫的集中URL的寫法
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/wyswust1","root","000000");
//Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/wyswust1?user=root&password=djl91600");
// Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/wyswust1?useUnicode=true&characterEncoding=utf-8&useSSL=false&" + "user=root&password=000000");
out.println("Database connected");
out.println("<br>");
Statement statement=con.createStatement();
ResultSet resultSet=statement.executeQuery("select market_price,shop_price,pimage from wyswust1.product where pname='HTC One M9+' or pid=1");
while(resultSet.next())
{
out.println(resultSet.getString("market_price")+"\t"+resultSet.getString("shop_price")+"\t"+resultSet.getString("pimage"));
//out.println(resultSet.getString(1)+"\t"+resultSet.getString(2)+"\t"+resultSet.getString(3));
out.println("<br>");
}
resultSet.close();
con.close();
}
catch(Exception e)
{
}
%>
</body>
</html>