jsp 连接 mysql 数据库

方法一:
jdbc
下载地址:
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.1.8.tar.gz/from/http://mirrors.sunsite.dk/mysql/

解压mysql-connector-java-3.1.8.tar.gz,把mysql-connector-java-3.1.8-bin.jar 放在/usr/loacal/下,将/usr/loacal/mysql-connector-java-3.1.8-bin.jar 加入到CLASSPATH 中

测试页面:

<%@ page contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*"%>

<%

Connection conn;
Statement stmt;
ResultSet rs;



Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql?user=root&password= &useUnicode=true&characterEncoding=gb2312");

stmt = conn.createStatement();

rs = stmt.executeQuery("select * from user");

%>
<table border="1" cellspace ="0" align="center">
<tr><td>DB CONNECT OK </td></tr>
<% while(rs.next()) {%>
<tr>
<td> <%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<%}%>
</table>

<%
rs.close();

stmt.close();

conn.close();
%>



方法二:

jdbc 连接器
http://dev.mysql.com/downloads/download.php?file=Downloads/Contrib/mm.mysql-2.0.4-bin.jar

将mm.mysql-2.0.4-bin.jar的路径加入classpath 即可
测试页面:


<%@page contentType="text/html;charset=gb2312"%>
<%

java.sql.Connection sqlConn;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

sqlConn=java.sql.DriverManager.getConnection
("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=gb2312", "root","");

sqlStmt=sqlConn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);

sqlRst=sqlStmt.executeQuery("select * from user");
%>
<html>
<head>
<title>Linux-JSP-JDBC-MYSQL TESTING -SELETCT </title>
</head>
<body>
<table border="1" cellspace ="0" align="center">
<tr><td>DB CONNECT OK </td></tr>
<% while(sqlRst.next()) {%>
<tr>
<td> <%=sqlRst.getString(1)%></td>
<td><%=sqlRst.getString(2)%></td>
</tr>
<%}%>
</table>
</body>
</html>
<%
sqlRst.close();
sqlStmt.close();
sqlConn.close();
%>

阅读更多
下一篇Window2003下Apache2.0.55+Tomcat5.5.16+PHP5.1.2的安装整合
想对作者说点什么? 我来说一句

java jsp 连接mysql数据库 jar包

2011年03月16日 1.72MB 下载

JSP数据库编程指南

2010年01月31日 2.15MB 下载

没有更多推荐了,返回首页

关闭
关闭