时间:2021-07-01 10:21:17 帮助过:20人阅读
mysql的url:jdbc:mysql://localhost:3306/mydb
由三部分组成,每个部分中间使用冒号分隔
第一部分是jdbc,这是固定的;
第二部分是数据库的名称,那么连接mysql数据库,第二部分当然是mysql了;
第三部分由数据库厂商规定的,我们需要了解每个数据库厂商的要求,mysql的第三部分分别由数据库服务器的IP地址(localhost)、端口号(3306),以及DATABASE名称(mydb) 组成;
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt("name"));
}
public static Connection getConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mydb1"; return DriverManager.getConnection(url, "root", "123"); } |
//增 @Test public void insert() throws Exception { Connection con = getConnection(); Statement stmt = con.createStatement(); String sql = "insert into user values(‘zhangSan‘, ‘123‘)"; stmt.executeUpdate(sql); System.out.println("插入成功!"); } |
//改 @Test public void update() throws Exception { Connection con = getConnection(); Statement stmt = con.createStatement(); String sql = "update user set password=‘456‘ where username=‘zhangSan‘"; stmt.executeUpdate(sql); System.out.println("修改成功!"); } |
//删 @Test public void delete() throws Exception { Connection con = getConnection(); Statement stmt = con.createStatement(); String sql = "delete from user where username=‘zhangSan‘"; stmt.executeUpdate(sql); System.out.println("删除成功!"); } |
//查 @Test public void query() throws Exception { Connection con = getConnection(); Statement stmt = con.createStatement(); String sql = "select * from user"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { String username = rs.getString(1); String password = rs.getString(2); System.out.println(username + ", " + password); } } |
JDBC小结
标签:otto out 个数 编写 print exec style 示例 bsp