MysqlTest.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.kmall.common;
  2. /*
  3. * 创建时间:2017-08-29 19:35
  4. * 项目名称:common_utils
  5. * 类名称:UpdatePassWord.java
  6. * 包名称:com.joyintech
  7. *
  8. * 修改履历:
  9. * 日期 修正者 主要内容
  10. *
  11. *
  12. * Copyright (c) 2016-2017 兆尹科技
  13. */
  14. import java.sql.*;
  15. /**
  16. * 名称:UpdatePassWord <br>
  17. * 描述:批量更新密码<br>
  18. * 注意事项:执行此类会把所有的用户(SYS_USER)密码初始化为a111111
  19. *
  20. * @author Scott
  21. * @version 1.0
  22. * @since 1.0.0
  23. */
  24. public class MysqlTest {
  25. /**
  26. * 数据库url
  27. */
  28. private static final String URL = "jdbc:mysql://45.248.68.244:3306/platform_security";
  29. /**
  30. * 用户名
  31. */
  32. private static final String USER = "root";
  33. /**
  34. * 密码
  35. */
  36. private static final String PASSWORD = "mysql123456";
  37. /**
  38. * mysql驱动程序
  39. */
  40. private static final String ORACLEDRIVER = "com.mysql.jdbc.Driver";
  41. /**
  42. * 启动程序
  43. *
  44. * @param args
  45. */
  46. public static void main(String[] args) {
  47. Connection con = null;
  48. PreparedStatement pre = null;
  49. ResultSet result = null;
  50. try {
  51. Class.forName(ORACLEDRIVER);
  52. con = DriverManager.getConnection(URL, USER, PASSWORD);
  53. System.out.println("连接成功!");
  54. String sql = "select * from mall_user";
  55. pre = con.prepareStatement(sql);
  56. result = pre.executeQuery();//执行查询
  57. while (result.next()) {
  58. // 当结果集不为空时
  59. String username = result.getString("username");
  60. String mobile = result.getString("mobile");
  61. System.out.println("会员:"+username + ";手机号:" +mobile);
  62. }
  63. } catch (ClassNotFoundException e) {
  64. //数据库驱动类异常处理
  65. System.out.println("Sorry,can`t find the Driver!");
  66. e.printStackTrace();
  67. } catch (SQLException e) {
  68. //数据库连接失败异常处理
  69. e.printStackTrace();
  70. } catch (Exception e) {
  71. // TODO: handle exception
  72. e.printStackTrace();
  73. } finally {
  74. try {
  75. if (result != null) {
  76. result.close();
  77. }
  78. if (pre != null) {
  79. pre.close();
  80. }
  81. if (con != null) {
  82. con.close();
  83. }
  84. System.out.println("数据库连接已关闭!");
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90. }