欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ubuntu 下java连接mysql数据库

程序员文章站 2022-05-27 09:29:09
...

这篇文章是对的
https://m.linuxidc.com/Linux/2008-10/16872.htm
还有这篇,忽略报错useSSL=false
https://blog.csdn.net/qq_41785135/article/details/85118329

剩下的就是安装mysql和创建数据库
打开数据库
mysql -u root -p
创建数据库
create database lalalala;记得最后有分号

给出我的代码

import java.sql.*;
import java.util.Properties;

public class DBDemo
{
  // The JDBC Connector Class.
  private static final String dbClassName = "com.mysql.jdbc.Driver";

  // Connection string. emotherearth is the database the program
  // is connecting to. You can include user and password after this
  // by adding (say) ?user=paulr&password=paulr. Not recommended!

  private static final String CONNECTION =
                          "jdbc:mysql://127.0.0.1/peak_db?useSSL=false";

  public static void main(String[] args) throws
                             ClassNotFoundException,SQLException
  {
    System.out.println(dbClassName);
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    Class.forName(dbClassName);

    // Properties for user and password. Here the user and password are both 'paulr'
    Properties p = new Properties();
    p.put("user","root");
    p.put("password","123123");

    // Now try to connect
    Connection c = DriverManager.getConnection(CONNECTION,p);

    System.out.println("It works !");
    c.close();
    }
}
相关标签: java