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);

    }

    }

    }

  • 相关阅读:
    获取CheckBox的Text值
    动态绑定数据至Html Table
    二次事件并细化功能
    ASP.NET MVC的JavaScriptResult
    Google Earth 8.0
    ASP.NET MVC的ContentResult
    ASP.NET MVC使用input标签上传文件
    处理动态SQL语句的参数
    Infor SyteLine如何快速锁定用户
    执行git push出现"Everything up-to-date"
  • 原文地址:https://www.cnblogs.com/CSAH/p/11638641.html
Copyright © 2011-2022 走看看