java mysql数据库的连接
发布网友
发布时间:2022-04-10 06:05
我来回答
共1个回答
热心网友
时间:2022-04-10 07:35
估计是你写的sql语句有问题了
最好吧代码贴出来啊
例子:
<%@ 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");
}%>