首先去官网下载jar包 在项目里面导入
1 //加载驱动 固定写法 2 Class.forName("com.mysql.jdbc.Driver"); 3 //用户信息 用户名密码 4 String url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true"; 5 String useName = "root"; 6 String usePassWord = "root 7 8 //连接成功,数据库对象 9 Connection connection = DriverManager.getConnection(url, useName, usePassWord); 10 11 //执行SQL对象 12 Statement statement = connection.createStatement(); 13 14 //执行SQL对象去执行SQl语句 15 String sql = "select*from student"; 16 ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){ System.out.println("id="+resultSet.getObject("id")); System.out.println("name="+resultSet.getObject("name")); System.out.println("age="+resultSet.getObject("age")); System.out.println("============================================="); 17 } 18 19 //释放连接resultSet.close(); 20 statement.close(); 21 connection.close();
整体大概流程