zoukankan      html  css  js  c++  java
  • 使用java连接MySQL数据库

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import com.mysql.jdbc.Driver;

    public class GetConn {
     Connection conn=null;
     static{
      try {
       Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
       System.out.println("数据库驱动加载成功!---MySQL");
      } catch (ClassNotFoundException e) {
       e.printStackTrace();
      }
     }
     public Connection getConn(){
      String url="jdbc:mysql://localhost:3306/estore-0315";
      String userName="root";
      String passWord="123";
      try {
       conn=DriverManager.getConnection(url, userName, passWord);//获取数据库连接
       if(conn!=null){
        System.out.println("数据库连接成功!---MySQL");
       }
      } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }  
      return conn;//返回Connection对象
     }
     public static void main(String[] args) {
      GetConn getConn=new GetConn();//创建GetConn对象
      Connection conn=getConn.getConn();
     }
    }

    前提是项目中一定要导入mysql-connector-java-5.1.26-bin.jar这个jar包。

    url格式:jdbc:mysql://[serverName[instanceName][:portNumber]][;property=value[;property=value]]

  • 相关阅读:
    js 数组详解(javascript array)
    CentOS 修改IP地址, DNS, 网关
    Leetcode 652.寻找重复的子树
    Leetcode 650.只有两个键的键盘
    Leetcode 649.Dota2参议院
    Leetcode 648.单词替换
    Leetcode 647.回文子串
    Leetcode 645.最长数对链
    Leetcode 643.子数组最大平均数I
    Leetcode 640.求解方程
  • 原文地址:https://www.cnblogs.com/Alex92/p/5139756.html
Copyright © 2011-2022 走看看