求一段jsp连接mysql的测试程序

求一段jsp连接mysql的测试程序,第1张

在此之前我的所有配置操作都成功了

就到了这里出现了问题

这里是哪里啊?

String url ="jdbc:mysql:

//localhost/softforum?user=

soft&password=soft1234&useUnicode=

true&characterEncoding=8859_1"

?softforum是你的数据库名吗?

select * from first ?

你的数据库有first表吗?

1.先装好JDK ex:C:\j2sdk1.4.2

2.再装TomCat ex:C:\Tomcat 5.0

3.下载 JDBC 驱动 

 http://mysql.easynet.be/Downloads/Connector-J/mysql-connector-java-3.1.14.zip

 下载就直接解压,里面有一个 mysql-connector-java-3.1.14-bin.jar 文件

 在 C:\ 建立一个 mysqlforjdbc 文件夹,将 mysql-connector-java-3.1.14-bin.jar 复制到这个文件夹中.

 再将 mysql-connector-java-3.1.14-bin.jar 复制到 C:\j2sdk1.4.2\lib 中

 再将 mysql-connector-java-3.1.14-bin.jar 复制到 C:\Tomcat 5.0\common\lib 和 C:\Tomcat 5.0\shared\lib 中

4.设置环境变量

classpath .d:\j2sdk\lib\dt.jard:\j2sdk\lib\tools.jard:\mysqlforjdbc\mysql-connector-java-3.1.14-bin.jard:\j2sdk\lib\mysql-connector-java-3.1.14-bin.jard:\mysqlforjdbc\msbase.jard:\mysqlforjdbc\mssqlserver.jard:\mysqlforjdbc\msutil.jar%CATALINA_HOME%\common\lib\servlet.jar

JAVA_HOME d:\j2sdk

Path %JAVA_HOME%\bin\

5.执行代码,输出结果在 C:\Tomcat 5.0\webapps\ROOT 中新建 test_mysql.jsp 测试文件<%@ page contentType="text/htmlcharset=gb2312" %><%@ page language="java" %><%@ page import="com.mysql.jdbc.Driver" %><%@ page import="java.sql.*" %>

<%//驱动程序名String driverName="com.mysql.jdbc.Driver"//数据库用户名String userName="root"//密码String userPasswd=""//数据库名String dbName="ex"//表名String tableName="post"//联结字符串String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswdClass.forName("com.mysql.jdbc.Driver").newInstance()Connection connection=DriverManager.getConnection(url)Statement statement = connection.createStatement()String sql="SELECT * FROM "+tableNameResultSet rs = statement.executeQuery(sql)//获得数据结果集合ResultSetMetaData rmeta = rs.getMetaData()//确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount()

out.print("id")out.print("| ")out.print("title")out.print("| ")out.print("comment ")out.print("<br>")while(rs.next()) {out.print(rs.getString(1))out.print(" | ")out.print(rs.getString(2))out.print(" | ")out.print(rs.getString(3))out.print("<br>")} out.print("<br>")out.print("数据库操作成功,恭喜你")rs.close()statement.close()connection.close()%>

mysql-connector-java-3.1.14-bin.jar 这个包

http://mysql.easynet.be/Downloads/Connector-J/mysql-connector-java-3.1.14.zip

环境变量

classpath .d:\j2sdk\lib\dt.jard:\j2sdk\lib\tools.jard:\mysqlforjdbc\mysql-connector-java-3.1.14-bin.jard:\j2sdk\lib\mysql-connector-java-3.1.14-bin.jard:\mysqlforjdbc\msbase.jard:\mysqlforjdbc\mssqlserver.jard:\mysqlforjdbc\msutil.jar

JAVA_HOME d:\j2sdk

Path %JAVA_HOME%\bin\

执行代码,输出结果在 C:\Tomcat 5.0\webapps\ROOT 中新建 test_mysql.jsp 测试文件<%@ page contentType="text/htmlcharset=gb2312" %><%@ page language="java" %><%@ page import="com.mysql.jdbc.Driver" %><%@ page import="java.sql.*" %>

<%//驱动程序名String driverName="com.mysql.jdbc.Driver"//数据库用户名String userName="root"//密码String userPasswd=""//数据库名String dbName="ex"//表名String tableName="post"//联结字符串String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswdClass.forName("com.mysql.jdbc.Driver").newInstance()Connection connection=DriverManager.getConnection(url)Statement statement = connection.createStatement()String sql="SELECT * FROM "+tableNameResultSet rs = statement.executeQuery(sql)//获得数据结果集合ResultSetMetaData rmeta = rs.getMetaData()//确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount()

out.print("id")out.print("| ")out.print("title")out.print("| ")out.print("comment ")out.print("<br>")while(rs.next()) {out.print(rs.getString(1))out.print(" | ")out.print(rs.getString(2))out.print(" | ")out.print(rs.getString(3))out.print("<br>")} out.print("<br>")out.print("数据库操作成功,恭喜你")rs.close()statement.close()connection.close()%>


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/54943.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-02-26
下一篇2023-02-26

发表评论

登录后才能评论

评论列表(0条)

    保存