在eclipse里建立jsp页面连接MySQL数据库

首先安装MySql数据库,并将数据库驱动程序文件 mysql-connector-java-3.1.12-bin.jar 放在 Tomcat common/lib 中。
在MySQL的登陆用户名:root,密码:mysql
建立数据库testdb,表user
 
基本代码如下:
 
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page language="java" import="java.sql.Statement" import="java.sql.*" %>
<html>
<head>
<title>helloworld.jsp</title>
</head>
<body>
<%
 try{
 Class.forName("org.gjt.mm.mysql.Driver");
 out.println("加载Mysql Driver成功!<br>");
 }
 catch(Exception e)
 {
 out.println("加载Mysql Driver失败!<br>");
 e.printStackTrace();
 }
 
 try{
 out.println("开始连接Mysql server!<br>");
 Connection connect=DriverManager.getConnection("jdbc:mysql://localhost/testdb?user=root&password=mysql&useUnicode=true&characterEncoding=8859_1");
 //jdbc:mysql://localhost/testdb?user=root&password=mysql&useUnicode=true&characterEncoding=8859_1
 //jdbc:mysql://localhost/数据库名user=数据库登陆用户名&password=数据库登陆密码&useUnicode=是否允许设置字符编码&characterEncoding=允许设置编码时,要设置的编码;
 out.print("成功连接Mysql server!<br><br>");
 
   Statement stmt = connect.createStatement();
   ResultSet rs=stmt.executeQuery("select * from user");
   out.print("读取数据如下:<br>");
   while(rs.next())
   {
    out.println(rs.getInt(1));
    out.println(rs.getString(2)+"<br>");
 }
 }
 catch(Exception e)
 {
 out.print("获得数据错误!");
 e.printStackTrace();
 }
%>
</body>
</html>
 
注意:如果只写import="java.sql.*" ,不写import="java.sql.Statement" 则在Statement stmt = connect.createStatement();其中Statement下面显示是红色波浪线,但程序调用数据库等一切正常。
 
 
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
采用Eclipse 编写的关于省市县三级菜单采用JSP +servlet+MYSQL ,数据库表的数据文件在src文件夹中 mysql> show tables; +----------------+ | Tables_in_area | +----------------+ | area | | cities | | provinces | +----------------+ 3 rows in set (0.00 sec) mysql> desc provinces; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | pid | char(6) | NO | | NULL | | | province | varchar(100) | NO | | | | +----------+--------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> desc cities; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | cid | char(6) | NO | | NULL | | | city | varchar(40) | NO | | NULL | | | pid | char(6) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> desc area; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | aid | char(6) | NO | | NULL | | | area | varchar(40) | NO | | NULL | | | cid | char(6) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值