MySQL数据库
IDEA 方面
jar下载
下载
https://dev.mysql.com/downloads/connector/j/
导入jar包
解压之后再导入
加载完成,查看
在2中添加所需的表
代码测试
package com.mysqlTest.chen;
import java.sql.*;
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
String username = "cwq1";
String password = "chen";
//3.连接成功,数据库对象Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//4.执行SQL的对象 Statement 执行sql的对象
Statement statement = connection.createStatement();
//5.执行语句
String sql = "SELECT * FROM grade";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println("gradeid=" + resultSet.getObject("gradeid"));
System.out.println("gradename=" + resultSet.getObject("gradename"));
System.out.println("===========");
}
//6.关闭
resultSet.close();
statement.close();
connection.close();
}
}
改进:
- db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/