怎么连接本地数据库mysql
MySQL是一种广泛使用的关系型数据库管理系统,它是一个开放源代码的软件,因此使用它非常方便。
连接到MySQL数据库需要以下步骤:
//导入MySQL驱动程序 import java.sql.*; //在Java中连接到MySQL数据库 public class ConnectMySQL { public static void main(String[] args) { Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "mydatabase"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "password"; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url + dbName, userName, password); System.out.println("Connected to the database"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { e.printStackTrace(); } } }