tomcat9.0+eclipse ee+jdbc配置

1.首先准备mysql 和eclipse环境,在环境搭建好之后,从eclipse官网下载jdbc的驱动包,下载地址https://dev.mysql.com/downloads/connector/j/

选择这个

 

2.解压它,拷贝mysql-connector-java-8.0.12.jar文件放到工程中我的是放在tomcat/lib/    下

,并导入路径

方法:右击工程名->Build Path->Configure Build Path,选择Add External JAR… 找到mysql-connector-java-8.0.12.jar所在的位置,然后将驱动包加载到项目中, 

 

像上面这样

 

 

 

然后将jdbc添加到web项目中

 

 

把命令行cmd切换到mysql的安装目录下的bin

1.      mysql -u root -p

2.  输入密码

3.    创建database:      create database RUNOOB;

4.   use   RUNOOB;

5.               CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',`url` varchar(255) NOT NULL DEFAULT '',`alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',  `country` char(10) NOT NULL DEFAULT '' COMMENT '国家',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

 

6.开启mysql服务

 

成功测试java代码:

 
import java.sql.*;
 
public class MySQLDemo {
 
    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    //"jdbc:mysql://localhost:3306/RUNOOB";
    static final String DB_URL = 
    "jdbc:mysql://localhost:3306/RUNOOB?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true";
    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "123456";
 
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");
        
            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
        
            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);
        
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    
}
}

 

 

jsp测试代码:

<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
Connection con;
Statement sql;
ResultSet rs;
try{Class.forName("com.mysql.cj.jdbc.Driver").newInstance();}
catch(Exception e){out.print(e);}
try{
    String uri="jdbc:mysql://localhost:3306/RUNOOB?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true";
    con=DriverManager.getConnection(uri,"root","123456");
    sql=con.createStatement();
    rs=sql.executeQuery("SELECT id, name, url FROM websites");
    
    // 展开结果集数据库
    while(rs.next()){
        // 通过字段检索
        int id  = rs.getInt("id");
        String name = rs.getString("name");
        String url = rs.getString("url");

        // 输出数据
       out.print("ID: " + id+"<br>");
     out.print(", 站点名称: " + name+"<br>");
       out.print(", 站点 URL: " + url+"<br>");
        out.print("<br>");
    }
    // 完成后关闭
     out.print("ok!");
    con.close();
}
catch(SQLException e1){out.print(e1);}
%>
</body>
</html>

 

 

错误提示:

 

遇到了驱动问题、SSL安全访问的问题和时区问题。 
报错1:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
1
高版本的驱动已经由

"com.mysql.jdbc.Driver"
1
变更为

private static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
1
报错2:

Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
创建连接失败,查看控制台异常信息
1
2
这个是安全连接的问题,在sql连接字符串中,添加关于不使用SSL访问数据数据库的说明:useSSL=false。

报错3:

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
1
这个是时区问题,在访问字符串中添加关于时区的设置说明:serverTimezone=UTC。

完整的数据库访问字符串为:

private static final String DB_URL = "jdbc:mysql://localhost:3306/jdbc_example?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值