jsp 连接MySQL实例

jsp 连接MySQL数据库:

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

<%@ page language = "java" %>
<%@ page import ="java.sql.*"%>
<!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>数据库测试</title>
</head>
<body>
hello


<table border="1" align="center"> 
<tr>
     <th>书号</th>
     <th>书名</th>
     <th>作者</th>
     <th>出版社</th>
</tr> 
</table>


<% java.util.Date d = new java.util.Date(); %>


Today's date is <%= d.toString() %> and this jsp page worked!




编号    姓名    地址
 <%
 Class.forName("com.mysql.jdbc.Driver").newInstance();
String         url = "jdbc:mysql://localhost/test";
String        user = "root";                              //MySQ帐号 
String    password = "admin";                             //MYSQL密码
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from test_table";




ResultSet rs=stmt.executeQuery(sql);


while(rs.next()) {
%>   


<table border="1" align=""> 
       <tr>
          <th><%=rs.getString(1)%></th>
          <th><%=rs.getString(2)%></th>
          <th><%=rs.getString(3)%></th>
       </tr> 
</table>
<%}%>   
<%out.print("数据库操作成功,恭喜你");%>   
<%rs.close();
stmt.close();
conn.close();
%>  
 
</body>
</html>



你可能感兴趣的:(jsp 连接MySQL实例)