JDBC连接mysql数据库

```

Connection connection = null;

Statement stmt = null;

ResultSet rs = null;

try {

// 注册驱动

DriverManager.registerDriver(new Driver());

// 连接数据库,参数1,mysql的地址,参数2为用户名,参数3为密码

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=true", "root", "root");

// 得到一个操作数据库的对象

stmt = connection.createStatement();

String sql = "select * from key_target";

// 执行sql语句,得到一个结果

rs = stmt.executeQuery(sql);

// 取值

while (rs.next()) {

String uid = rs.getString("type");

System.out.println(uid);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

//关闭连接对象

try {

if (connection != null) {

connection.close();

}

if (stmt != null) {

stmt.close();

}

if (rs != null) {

rs.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

```

你可能感兴趣的:(JDBC连接mysql数据库)