ubuntu 下java连接mysql数据库
Posted Hill_LAI
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu 下java连接mysql数据库相关的知识,希望对你有一定的参考价值。
这篇文章是对的
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();
}
}
以上是关于ubuntu 下java连接mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
mysql 错 Could not open JDBC Connection for transaction; nested exception is java.sql.SQLExceptio(代码片
ubuntu18安装MySQL5.7和可视化Navicat,intellIj IDEA配置JDBC连接Java的jar包