diff --git "a/05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" "b/05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" new file mode 100644 index 0000000000000000000000000000000000000000..9e98393f3d93a22bc4b3684912066c81c7132ac0 --- /dev/null +++ "b/05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" @@ -0,0 +1,174 @@ +# 作业 + +### 查询 + +```java +import java.sql.*; +public class select { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url ="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql="select * from sysy"; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + ResultSet rs = st.executeQuery(sql); + //6.处理返回的结果 + while (rs.next()){ + //rs结果中有,三个字段 id name money + System.out.println(rs.getInt("编号")+ + "\t"+ rs.getString("姓名")+ + "\t"+rs.getInt("工资")); + } + //7.释放资源 + st.close(); + rs.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +### 修改 + +```java +import java.sql.*; + +public class update { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url ="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql="UPDATE sysy SET 姓名='小臂崽子'where 编号=5"; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + int rs = st.executeUpdate(sql); + //6.处理返回的结果 + if (rs>0){ + System.out.println("修改成功-------------------------"); + }else { + System.out.println("修改失败-------------------------"); + } + //7.释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + +``` + +### 增加 + +```java +import java.sql.*; + +public class insert { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql = "insert into sysy values(6,'小鸡崽子',2000) "; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + int rs = st.executeUpdate(sql); + + //6.处理返回的结果 + + if (rs>0){ + System.out.println("添加成功-----------------------"); + }else { + System.out.println("添加失败------------------------"); + + } + + //7.释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + + + +``` + +### 删除 + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class delete { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url ="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql="DELETE FROM sysy WHERE 编号=6"; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + int rs = st.executeUpdate(sql); + //6.处理返回的结果 + if (rs>0){ + System.out.println("删除成功-------------------------"); + }else { + System.out.println("删除失败-------------------------"); + } + //7.释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + +``` +