就到了这里出现了问题
这里是哪里啊?
String url ="jdbc:mysql:
//localhost/softforum?user=
soft&password=soft1234&useUnicode=
true&characterEncoding=8859_1"
select * from first ?
你的数据库有first表吗?
1.先装好JDK ex:C:\j2sdk1.4.22.再装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()%>
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)