[关闭]
@martin0207 2018-04-14T01:12:39.000000Z 字数 2755 阅读 808

数据库——JDBC

Java学习


前序

学习Java的路上,一步步走到数据库这里,当然选择了大多数人会选择的MySql数据库。
安装教程
教程里已经写的非常详细,就不再赘述。

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序

使用

1、下载MySqlConnector

安装与解压版的MySql并没有JDBC的jar包,需要在官网重新下载。
官网首页 → downloads → MYSQL Connectors(左侧选择列表) → Connector/J
使用-bin文件

2、简单使用

  1. public static void query() {
  2. /*
  3. * 1.创建Connection 需要指定相应的连接字符串,用户名和密码
  4. */
  5. String url = "jdbc:mysql://localhost:3306/db_test?useSSL=true";
  6. String user = "test";
  7. String password = "123456";
  8. Connection connection = null;
  9. Statement statement = null;
  10. ResultSet set = null;
  11. try {
  12. connection = DriverManager.getConnection(url, user, password);
  13. statement = connection.createStatement();
  14. /**
  15. * 2.创建sql语句 执行SQL语句,需要使用Statement
  16. */
  17. String sql = "select * from user";
  18. /**
  19. * 增加或更新数据时,使用 excuteUpdate(sql);
  20. */
  21. //sql = "insert into user (name) value ('插入用户')";
  22. //sql = "update user set pwd='150', address='adress' where id=6";
  23. //statement.executeUpdate(sql);
  24. /**
  25. * 在获取的 statement之后, 如果执行更新,直接使用state.executeUpdate(sql);
  26. * 如果是执行查询,需要通过state.executeQuery(sql)完成,返回是ResultSet记录集
  27. */
  28. set = statement.executeQuery(sql);
  29. /**
  30. * 遍历记录集的方式
  31. */
  32. while (set.next()) {
  33. System.out.println(set.getInt("id") + "," + set.getString("name"));
  34. /**
  35. * 也可以用数据库的投影顺序来访问字段,但是不建议,顺序可能会变
  36. * 一般都是使用字段名字访问
  37. */
  38. System.out.println(set.getInt(1)+"," + set.getString(2));
  39. }
  40. } catch (SQLException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. } finally {
  44. /**
  45. * 操作完成之后,需要释放连接 先释放 ResultSet 再释放 Statement 最后释放 Connection 倒叙释放
  46. */
  47. try {
  48. if (set != null)
  49. set.close();
  50. if (statement != null)
  51. statement.close();
  52. if (connection != null)
  53. connection.close();
  54. } catch (SQLException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. }
  59. }

如上即正常使用JDBC。
useSSL=true 如果不添加,IDEA编辑器会提示错误,但是不影响运行。

3、优化使用

Statement 使用时,存在安全问题:

登录语句:select count() from user where name='name' and pwd='123';
用户名输入 admin"';// 时,查询语句为:select count(
) from user where name='admin"';// and pwd='123';
这就尴尬了,系统认为,查询语句是select count(*) from user where name='admin"';如果系统内有admin这个账号时,肯定登录成功—— SQL注入攻击

所以,我们在使用时,并不用Statement,而是使用PreparedStatement:

  1. public static void insert2() {
  2. String url = "jdbc:mysql://localhost:3306/db_test?useSSL=true";
  3. String user = "test";
  4. String password = "123456";
  5. String sql = "";
  6. Connection connection = null;
  7. PreparedStatement statement = null;
  8. try {
  9. connection = DriverManager.getConnection(url, user, password);
  10. sql = "insert into user (name) value ('query2')";
  11. // sql = "update user set pwd='150', address='adress' where id=6";
  12. statement = connection.prepareStatement(sql);
  13. statement.executeUpdate();
  14. /*
  15. // 可以使用PreparedStatement 代替Statement,这样可以有效的解决SQL注入攻击的问题
  16. PreparedStatement pStatement = connection.prepareStatement(sql);
  17. pStatement.setInt(1, 5);
  18. set = pStatement.executeQuery();
  19. */
  20. } catch (SQLException e) {
  21. // TODO Auto-generated catch block
  22. e.printStackTrace();
  23. } finally {
  24. try {
  25. if (connection != null)
  26. connection.close();
  27. if (statement != null)
  28. statement.close();
  29. } catch (SQLException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. }
  34. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注