2阶——JDBC
时间:2021-07-01 10:21:17
帮助过:16人阅读
cn.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.*
;
public class Demo1 {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/day11"
;
String username = "root"
;
String password = "123456"
;
try {
Class.forName("com.mysql.jdbc.Driver"
); //将Driver类加载到内存 就会自动执行以下的静态代码块
// com.mysql.jdbc.Driver 通过查看源码Driver中含有以下静态代码块
// static {
// try {
// DriverManager.registerDriver(new Driver());
// } catch (SQLException var1) {
// throw new RuntimeException("Can‘t register driver!");
// }
// }
Connection cn =
DriverManager.getConnection(url,username,password);
String sql = "select * from day11"
;
PreparedStatement sta =
cn.prepareStatement(sql);
ResultSet rs =
sta.executeQuery();
while (rs.next()){
int id = rs.getInt(1
);
String name = rs.getString(2
);
System.out.println(id+"___"+
name);
}
rs.close();
sta.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2阶——JDBC
标签:trace 关系型数据库 host manager ace dstat span 数据库 实现