diff --git "a/53 \345\221\250\345\216\232\350\276\260/20230517 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232 JDBC.md" "b/53 \345\221\250\345\216\232\350\276\260/20230517 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232 JDBC.md" new file mode 100644 index 0000000000000000000000000000000000000000..0f5a300dc3c162fc4c2e6ab15bd6a617b205b6c2 --- /dev/null +++ "b/53 \345\221\250\345\216\232\350\276\260/20230517 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232 JDBC.md" @@ -0,0 +1,182 @@ +# 20230517 第一次作业 JDBC + +## (Java连接MySQL数据库) + +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + + 2. 能否实现将查询的结果,封装成java对象 + +## 答案 + +```java +import java.sql.*; +/* +查询 +* */ +public class Select { + public static final String Driver = "com.mysql.jdbc.Driver"; + public static final String URL = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + public static final String user = "root"; + public static final String password = "root"; + + public static void main(String[] args) { + try { + Class.forName(Driver); + Connection conn= DriverManager.getConnection(URL,user,password); +// System.out.println(conn); + Statement sta=conn.createStatement(); + String sql="SELECT id,name,sex from student"; + ResultSet resultSet = sta.executeQuery(sql); + while (resultSet.next()){ + int id=resultSet.getInt("id"); + String name=resultSet.getString("name"); + String sex=resultSet.getString("name"); + System.out.println("ID:"+id+"\tname:"+name+"\tsex:"+sex); + + } + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} +``` + +```java +package java与数据库的连接; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +/* +更新 +* */ +public class Update { + public static final String Driver = "com.mysql.jdbc.Driver"; + public static final String URL = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + public static final String user = "root"; + public static final String password = "root"; + + public static void main(String[] args) { + try { + Class.forName(Driver); + Connection conn= DriverManager.getConnection(URL,user,password); + Statement sta=conn.createStatement(); + String sql="update student set name='zs' where id=3"; + int i =sta.executeUpdate(sql); + if (i==1) { + System.out.println("修改成功"); + } else { + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("DriverError"); + } catch (SQLException e) { + System.out.println("SQL语句错误"); + } + } +} +``` + +```java +package java与数据库的连接; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +/* +删除 +*/ +public class Delete { + public static final String Driver = "com.mysql.jdbc.Driver"; + public static final String URL = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + public static final String user = "root"; + public static final String password = "root"; + + public static void main(String[] args) { + try { + Class.forName(Driver); + Connection conn = DriverManager.getConnection(URL, user, password); + Statement sta = conn.createStatement(); + String sql = "delete from student where id=2"; + int i = sta.executeUpdate(sql); + if (i == 1) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("DriverError"); + } catch (SQLException e) { + System.out.println("SQL语句错误"); + } + + } +} +``` + +```java +package java与数据库的连接; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +/* +增加 + */ +public class Insert { + public static final String Driver = "com.mysql.jdbc.Driver"; + public static final String URL = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + public static final String user = "root"; + public static final String password = "root"; + + public static void main(String[] args) { + try { + Class.forName(Driver); + Connection conn= DriverManager.getConnection(URL,user,password); + Statement sta=conn.createStatement(); + String sql="insert into student values(4,'小明','男')"; + int i=sta.executeUpdate(sql); + if (i==1) { + System.out.println("插入成功"); + } else { + System.out.println("插入失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("DeriveError"); + } catch (SQLException e) { + System.out.println("SQL语句错误"); + } + + } +} +``` +