Day7.7.rar
javaweb.zip

Jdbc增删改查

  1. package com.xx.Jdbc.demo;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8. import org.junit.Test;
  9. import java.sql.Connection;
  10. public class Demo {
  11. /**
  12. * 查询
  13. */
  14. @Test
  15. public void Demo1() throws SQLException{
  16. Connection conn = null;
  17. Statement stmt = null;
  18. ResultSet rs = null;
  19. try {
  20. conn = JunitDemo.getConnection();
  21. stmt = conn.createStatement();
  22. String sql = "select * from t_user";
  23. rs = stmt.executeQuery(sql);
  24. while (rs.next()) {
  25. System.out.println("id:"+rs.getInt("uid")+"name:"+rs.getString("name")+"password:"+rs.getString("password"));
  26. }
  27. } catch (SQLException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }finally {
  31. JunitDemo.release(rs ,stmt, conn);
  32. }
  33. }
  34. /**
  35. * 更新
  36. */
  37. @Test
  38. public void Demo2() throws SQLException{
  39. Connection conn = null;
  40. Statement stmt = null;
  41. try {
  42. conn = JunitDemo.getConnection();
  43. stmt = conn.createStatement();
  44. String sql = "update t_user set password='xxxxxx' where uid=4";
  45. int num = stmt.executeUpdate(sql);
  46. if (num>0) {
  47. System.out.println("成功");
  48. }else {
  49. System.out.println("失败");
  50. }
  51. } catch (SQLException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }finally {
  55. JunitDemo.release(stmt, conn);
  56. }
  57. }
  58. /**
  59. * 添加
  60. */
  61. @Test
  62. public void Demo3() throws SQLException{
  63. Connection conn = null;
  64. Statement stmt = null;
  65. try {
  66. conn = JunitDemo.getConnection();
  67. stmt = conn.createStatement();
  68. String sql = "insert into t_user (name,password) values ('柒萧萧','qs12345')";
  69. int num = stmt.executeUpdate(sql);
  70. if (num>0) {
  71. System.out.println("成功");
  72. }else {
  73. System.out.println("失败");
  74. }
  75. } catch (SQLException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }finally {
  79. JunitDemo.release(stmt, conn);
  80. }
  81. }
  82. /**
  83. * 删除
  84. */
  85. @Test
  86. public void Demo4() throws SQLException{
  87. Connection conn = null;
  88. Statement stmt = null;
  89. try {
  90. conn = JunitDemo.getConnection();
  91. stmt = conn.createStatement();
  92. String sql = "delete from t_user where uid = 4";
  93. int num = stmt.executeUpdate(sql);
  94. if (num>0) {
  95. System.out.println("成功");
  96. }else {
  97. System.out.println("失败");
  98. }
  99. } catch (SQLException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }finally {
  103. JunitDemo.release(stmt, conn);
  104. }
  105. }
  106. /**
  107. * 登录
  108. */
  109. @Test
  110. public void Demo5() throws SQLException{
  111. Connection conn = null;
  112. Statement stmt = null;
  113. ResultSet rs = null;
  114. try {
  115. conn = JunitDemo.getConnection();
  116. stmt = conn.createStatement();
  117. Scanner scanner = new Scanner(System.in);
  118. System.out.println("用户名");
  119. String user = scanner.next();
  120. System.out.println("密码");
  121. String password = scanner.next();
  122. String sql = "select count(*) from t_user where name='"+user+"' and password='"+password+"'";
  123. rs = stmt.executeQuery(sql);
  124. while (rs.next()) {
  125. if (rs.getInt(1)>0) {
  126. System.out.println("成功");
  127. }else {
  128. System.out.println("失败");
  129. System.exit(0);
  130. return;
  131. }
  132. }
  133. } catch (SQLException e) {
  134. // TODO Auto-generated catch block
  135. e.printStackTrace();
  136. }finally {
  137. JunitDemo.release(stmt, conn);
  138. }
  139. }
  140. }

PreparedStatement之增删改查

  1. /**
  2. * PreparedStatement登录
  3. */
  4. @Test
  5. public void Demo6() throws SQLException{
  6. Connection conn = null;
  7. PreparedStatement pstmt = null;
  8. ResultSet rs = null;
  9. try {
  10. conn = JunitDemo.getConnection();
  11. Scanner scanner = new Scanner(System.in);
  12. System.out.print("用户名");
  13. String user = scanner.next();
  14. System.out.print("密码");
  15. String password = scanner.next();
  16. String sql = "select count(*) from t_user where name=? and password=?";
  17. // System.out.println(sql);
  18. pstmt = conn.prepareStatement(sql);
  19. pstmt.setString(1, user);
  20. pstmt.setString(2, password);
  21. rs = pstmt.executeQuery();
  22. while (rs.next()) {
  23. if (rs.getInt(1)>0) {
  24. System.out.println("成功");
  25. }else {
  26. System.out.println("失败");
  27. System.exit(0);
  28. return;
  29. }
  30. }
  31. } catch (SQLException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }finally {
  35. JunitDemo.release(rs, pstmt, conn);
  36. }
  37. }
  38. /**
  39. * PreparedStatement插入
  40. */
  41. @Test
  42. public void Demo7() throws SQLException{
  43. Connection conn = null;
  44. PreparedStatement pstmt = null;
  45. ResultSet rs = null;
  46. try {
  47. conn = JunitDemo.getConnection();
  48. Scanner scanner = new Scanner(System.in);
  49. System.out.println("用户名");
  50. String user = scanner.next();
  51. System.out.println("密码");
  52. String password = scanner.next();
  53. String sql = "insert into t_user (name,password) values (?,?)";
  54. System.out.println(sql);
  55. pstmt = conn.prepareStatement(sql);
  56. pstmt.setString(1, user);
  57. pstmt.setString(2, password);
  58. int num = pstmt.executeUpdate();
  59. if (num>0) {
  60. System.out.println("成功");
  61. }else {
  62. System.out.println("失败");
  63. System.exit(0);
  64. return;
  65. }
  66. } catch (SQLException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }finally {
  70. JunitDemo.release(pstmt, conn);
  71. }
  72. }
  73. /**
  74. * PreparedStatement删除
  75. */
  76. @Test
  77. public void Demo8() throws SQLException{
  78. Connection conn = null;
  79. PreparedStatement pstmt = null;
  80. ResultSet rs = null;
  81. try {
  82. conn = JunitDemo.getConnection();
  83. Scanner scanner = new Scanner(System.in);
  84. System.out.println("删除ID");
  85. int id = scanner.nextInt();
  86. String sql = "delete from t_user where id=?";
  87. // System.out.println(sql);
  88. pstmt = conn.prepareStatement(sql);
  89. pstmt.setInt(1, id);
  90. int num = pstmt.executeUpdate();
  91. if (num>0) {
  92. System.out.println("成功");
  93. }else {
  94. System.out.println("失败");
  95. System.exit(0);
  96. return;
  97. }
  98. } catch (SQLException e) {
  99. // TODO Auto-generated catch block
  100. e.printStackTrace();
  101. }finally {
  102. JunitDemo.release(pstmt, conn);
  103. }
  104. }
  105. /**
  106. * PreparedStatement更新
  107. */
  108. @Test
  109. public void Demo9() throws SQLException{
  110. Connection conn = null;
  111. PreparedStatement pstmt = null;
  112. ResultSet rs = null;
  113. try {
  114. conn = JunitDemo.getConnection();
  115. Scanner scanner = new Scanner(System.in);
  116. System.out.println("更新ID");
  117. int id = scanner.nextInt();
  118. System.out.println("用户名修改为");
  119. String up_name = scanner.next();
  120. String sql = "update t_user set name=? where id=?";
  121. // System.out.println(sql);
  122. pstmt = conn.prepareStatement(sql);
  123. pstmt.setString(1, up_name);
  124. pstmt.setInt(2, id);
  125. int num = pstmt.executeUpdate();
  126. if (num>0) {
  127. System.out.println("成功");
  128. }else {
  129. System.out.println("失败");
  130. System.exit(0);
  131. return;
  132. }
  133. } catch (SQLException e) {
  134. // TODO Auto-generated catch block
  135. e.printStackTrace();
  136. }finally {
  137. JunitDemo.release(pstmt, conn);
  138. }
  139. }

JdbcUtils工具类

  1. package com.xx.JdbcUtils.Demo;
  2. import java.sql.*;
  3. public class JdbcUtils {
  4. /**
  5. * 声明成员变量
  6. * */
  7. private static final String driverClassName;
  8. private static final String url;
  9. private static final String user;
  10. private static final String password;
  11. /**
  12. * 成员变量赋值
  13. * */
  14. static {
  15. driverClassName = "com.mysql.jdbc.Driver"; //mysql jar包
  16. url = "jdbc:mysql://localhost:3306/stusys";//数据库链接
  17. user = "root"; //数据库用户名
  18. password = "root"; //数据库密码
  19. }
  20. /**
  21. * 加载驱动
  22. * */
  23. public static void loadDriver() throws ClassNotFoundException {
  24. Class.forName(driverClassName);
  25. }
  26. /**
  27. * 获得链接
  28. * */
  29. public static Connection getConnection() throws ClassNotFoundException {
  30. loadDriver();//加载驱动
  31. Connection conn = null;
  32. try {
  33. conn = DriverManager.getConnection(url,user,password); //获得链接
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. return conn;
  38. }
  39. /**
  40. * 释放资源 Statement Connection
  41. **/
  42. public static void release(Statement stmt,Connection conn) throws SQLException {
  43. stmt.close();
  44. conn.close();
  45. }
  46. /**
  47. * 释放资源 ResultSet Statement Connection
  48. * */
  49. public static void release(ResultSet rs, Statement stmt, Connection conn) throws SQLException {
  50. rs.close();
  51. stmt.close();
  52. conn.close();
  53. }
  54. }

使用JdbcUtils工具类

  1. package day;
  2. import com.xx.JdbcUtils.Demo.JdbcUtils;
  3. import org.junit.jupiter.api.Test;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. public class Demo {
  9. //声明全局遍变量
  10. Connection conn;
  11. PreparedStatement pstmt;
  12. ResultSet rs;
  13. /**
  14. * 查询
  15. * */
  16. @Test
  17. public void demo() throws ClassNotFoundException, SQLException {
  18. String sql = "select * from sort";//编写sql语句
  19. conn = JdbcUtils.getConnection(); //获取链接 调取JdbcUtils工具类
  20. pstmt = conn.prepareStatement(sql);//执行sql语句
  21. rs = pstmt.executeQuery();
  22. //遍历集合
  23. while (rs.next()){
  24. //输出集合内容 ID user
  25. System.out.println(rs.getInt("id")+"++++"+rs.getString("user") );
  26. }
  27. }
  28. }

db.properties

  1. Properties properties = new Properties();
  2. properties.load(new FileInputStream("src/db.properties"));
  3. driverClassName = properties.getProperty("driverClassName");
  4. url = properties.getProperty("url");
  5. name = properties.getProperty("name");
  6. password = properties.getProperty("password");
  1. driverClassName=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/rubysiu
  3. name=root
  4. password=root

QueryRunner

  1. /**
  2. * 获取多条记录
  3. * @throws SQLException
  4. * */
  5. @Test
  6. public void getAll() throws SQLException {
  7. QueryRunner qr = new QueryRunner();
  8. String sql = "selcet * from Student";
  9. List<Student> list = qr.query(JunitDemo.getConnection(),sql,new BeanListHandler<Student>(Student.class));
  10. for (Student student : list) {
  11. System.out.println(student.toString());
  12. }
  13. }
  14. String sql = "selcet * from Student where name=?";
  1. /**
  2. * 获取一条记录
  3. * @throws SQLException
  4. * */
  5. @Test
  6. public void getOne() throws SQLException {
  7. Scanner scanner = new Scanner(System.in);
  8. System.out.println("用户名");
  9. String name = scanner.next();
  10. QueryRunner qr = new QueryRunner();
  11. String sql = "selcet * from Student where name=?";
  12. Object[] params={name};
  13. Student student = qr.query(JunitDemo.getConnection(), sql,new BeanHandler<Student>(Student.class),params);
  14. System.out.println(student.toString());
  15. }