diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" "b/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..cd4dc38024668772a1d3e5319ae6b47708ce8edc --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" @@ -0,0 +1,181 @@ +## 作业 + +## 数据库代码 + +```mysql +create database student_db CHARSET utf8; +use student_db; +CREATE table studen( + id int, + name varchar(10), + sex enum('男','女') +); +insert into studen values +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +``` + +## 查询 + +```java +public class Sele { + public static void main(String[] args) { + ResultSet re = null; + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "select * from studen"; + sta = con.createStatement(); + re = sta.executeQuery(sql); + while (re.next()) { + System.out.println(re.getInt("id") + ":" + re.getString("name") +" "+ re.getString("sex")); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + if (re != null) { + re.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } +} + +``` + + + +## 添加 + +```java +public class Inse { + public static void main(String[] args) { + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "insert into studen values(4,'赵六','男')"; + sta = con.createStatement(); + int i = sta.executeUpdate(sql); + System.out.println("添加成功!"); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + + + +## 修改 + +```java +public class Upda { + public static void main(String[] args) { + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "update studen set name='老六' where id=4"; + sta = con.createStatement(); + int i = sta.executeUpdate(sql); + System.out.println("修改成功!"); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + +## 删除 + +```java +public class Dele { + public static void main(String[] args) { + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "delete from studen where id=4"; + sta = con.createStatement(); + int i = sta.executeUpdate(sql); + System.out.println("删除成功!"); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` +