关于eclipse连接mysql数据库

来源:互联网 发布:windows server pack 编辑:程序博客网 时间:2024/05/01 07:19

我安装的是eclipse3.2,mysql5.0,tomcat5.0.28.相信大家安装eclipse,tomcat和mysql都没有问题。今天我把tomcat卸载了,重新安装,连接mysql数据库老是连接不上去,真老火。其实很简单:
  下载一个mysql-connector-java-3.1.11-bin.jar,把这个放在tomcat的common/lib下面,在eclipse里面重启tomcat就能连接mysql数据库了。下面是我的例子:

注意:要先自己在mysql里面创建一个数据库books,表book,以及属性名:bookID,bookName,publisher, price

<%@ page contentType="text/html; charset=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="200614";
//数据库名
String dbName="books";
//表名
String tableName="book";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
//获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rmeta.getColumnCount();
// 输出每一个数据值
out.print("id");
out.print("|");
out.print("num");
out.print("<br>");
while(rs.next()) {
out.print(rs.getString("bookId"));
out.print(rs.getString("bookName"));
out.print(rs.getString("publisher"));
out.print(rs.getString("price"));  
out.print("<br>");
}
out.print("<br>");
out.print("数据库操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>

如果有问题欢迎联系我QQ:251378436

原创粉丝点击