一个jsp连接mysql的简单例子

发表于:2007-07-04来源:作者:点击数: 标签:
%@ page language= java import=java.sql.*% % String mySqlDriver = org.gjt.mm. mysql .Driver; String url = jdbc:mysql://localhost/hq;//数据库连接 Connection conn; Class.forName(mySqlDriver); conn = DriverManager.getConnection( url,root,);//

<%@ page language="java" import="java.sql.*"%>
<%
String mySqlDriver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/hq";//数据库连接
Connection conn;
Class.forName(mySqlDriver);
conn = DriverManager.getConnection( url,"root","");//用户名与密码
if(conn==null){
System.out.println("get Conn Error");
}
Statement stmt=conn.createStatement();
ResultSet rs=null;
rs=stmt.executeQuery("select * from news_art");
while(rs.next())
{//循环显示记录
%>
<a href=''display.jsp?id=<%=rs.getString("id")%>'' target=_blank><%=rs.getString("title")%></a>(<%=rs.getString("times")%>)
<%="<br>"%>
<%
}
rs.close();
stmt.close();
conn.close();
%>

原文转自:http://www.ltesting.net