zoukankan      html  css  js  c++  java
  • 01、JDBC连接

    ① 下载JDBC-MySQL数据库驱动

    链接:https://pan.baidu.com/s/1KtSZZ5hXlxu2QrmsXg3lkA
    提取码:1pbl

    ② 加载JDBC-MySQL数据库驱动

    范例:MySQL数据库驱动加载

    Class.forNmae("com.mysql.jdbc.Driver");

    注:上面语句需要try catch捕获 或者throws异常

    ③ 连接数据库

       java.sql包中的DriverManager类有两个忠于建立连接的类方法(static方法)

    NO.

    方法名称

    类型

    描述

    01

    public static Connection getConnection(String url,Properties info)throws SQLException

    普通

    建立到给定数据库 URL 的连接

    02

    public static Connection getConnection(String url,String user,String password)throws SQLException

    普通

    建立到给定数据库 URL 的连接

    范例:连接数据库

    package com.hsp;

    import java.sql.Connection;

    import java.sql.DriverManager;

    public class testDatabase {

    /**

    * @param args

    */

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    Connection con = null;

    String DBname = "jdb"; //数据库名字

    //  String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true";

    //如果数据库的表中的记录有汉字,那么需要characterEnconding=gb2312 或utf-8 如果不清楚有没有函数推荐使用下列这个方法

    String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8";

    String username = "root";//数据库账号

    String password = "root";//数据库密码

    try {

    Class.forName("com.mysql.jdbc.Driver");

    con = DriverManager.getConnection(url, username, password);//连接代码

    System.out.println("输出con地址:"+con);

    } catch (Exception e) {

    // TODO: handle exception

    System.out.println(e);

    }

    }

    }

  • 相关阅读:
    JS定时执行,循环执行
    Ecshop(二次开发)
    百度歌曲接口
    给大家讲讲在哪些地方发外链最好
    360浏览器默认以兼容模式或急速模式方式打开页面
    子iframe 怎么调用 父级的JS函数
    ASP 发送邮件
    PHP发送邮件
    php表单数据验证类
    js获取url传递参数
  • 原文地址:https://www.cnblogs.com/HOsystem/p/11638641.html
Copyright © 2011-2022 走看看