java mysql数据库的连接

例子:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>display message</title>

<%   
   String url="jdbc:mysql://localhost:3306/shujukuming";

  try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection dcon=DriverManager.getConnection(url,"root","shujukumima");
    String sql="select * from message;";
 Statement stmt=dcon.createStatement();
 ResultSet rs=stmt.executeQuery(sql);

 if(rs.next())
 {

 int id=rs.getInt("id");
 String name=rs.getString("name");
 String title=rs.getString("title");
 String content=rs.getString("content");
 java.sql.Date messageDate=rs.getDate("messageDate");
%>
  <table width="249" border="1" align="center">
  <tr>
    <td width="111">name:<%=name%></td>
    <td width="122">time:<%=messageDate%></td>
  </tr>
  <tr>
    <td>title:<%=title%></td>
    <td>id:<%=id%></td>
  </tr>
  <tr>
    <td height="82" colspan="2"> <%=content%></td>
  </tr>
</table>
 <% }} catch(Exception e){
         out.print("wrong here");
}%>
摘自:http://zhidao.baidu.com/question/25015915.html

Leave a Reply