当前位置:   article > 正文

Java中的MySQLUtils_java mysqlutil

java mysqlutil
  1. package com.youngman.Server;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class MySQLUtils {
  8. public static Statement stmt = null;
  9. public static Connection conn = null;
  10. public static void connetDatabse() throws SQLException,
  11. ClassNotFoundException {
  12. Class.forName("com.mysql.jdbc.Driver");
  13. String url = "jdbc:mysql://localhost:3306/paoding";
  14. String user = "root";
  15. String password = "";
  16. if (conn != null) {
  17. conn.close();
  18. conn = null;
  19. }
  20. conn = DriverManager.getConnection(url, user, password);
  21. System.out.println("Connection is Successful");
  22. if (stmt != null) {
  23. stmt.close();
  24. stmt = null;
  25. }
  26. stmt = (Statement) conn.createStatement();
  27. }
  28. public static void closeConnet() throws SQLException {
  29. stmt.close();
  30. conn.close();
  31. }
  32. public static boolean isconnect() {
  33. if (stmt == null || conn == null) {
  34. System.out.println("MySQL Connet Fail");
  35. return false;
  36. } else {
  37. return true;
  38. }
  39. }
  40. public static void QueryAllData() throws SQLException {
  41. isconnect();
  42. String sql = "select * from userdata";
  43. ResultSet rs = stmt.executeQuery(sql);
  44. while (rs.next()) {
  45. System.out.println(rs.getInt(1) + "\t" + rs.getString("user_name")
  46. + "\t" + rs.getString("password") + "\t"
  47. + rs.getString("phone"));
  48. }
  49. rs.close();
  50. }
  51. public static boolean QuerySingle(String user_name) throws SQLException {
  52. String sql = "select * from userdata where user_name='" + user_name
  53. + "'";
  54. return QueryUser(sql);
  55. }
  56. public static boolean QuerySingle(String user_name, String password)
  57. throws SQLException {
  58. String sql = "select * from userdata where user_name='" + user_name
  59. + "' And password='" + password + "'";
  60. return QueryUser(sql);
  61. }
  62. public static boolean QueryUser(String sql) throws SQLException {
  63. isconnect();
  64. ResultSet rs = stmt.executeQuery(sql);
  65. if (rs.next()) {
  66. System.out.println("This user already exists");
  67. System.out.println(rs.getInt(1) + "\t" + rs.getString("user_name")
  68. + "\t" + rs.getString("password") + "\t"
  69. + rs.getString("phone"));
  70. rs.close();
  71. return true;
  72. }
  73. return false;
  74. }
  75. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/1006256
推荐阅读
相关标签
  

闽ICP备14008679号