JDK 1.8 Eclipse Mysql
package com.star.db; import java.sql.Connection; import java.sql.DriverManager; public class MyConn { public static Connection getConn() { Connection c = null; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //test为所连接的数据库,root为数据库用户名,abc123为密码 c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","abc123"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return c; } }
public static void main(String[] args) { MyConn mc = new MyConn(); System.out.println(mc.getConn()); }
运行结果