Guest User

Untitled

a guest
Sep 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. delete and insert not working in java with mysql
  2. package model;
  3.  
  4. import Beans.ChatLineBeans;
  5. import Beans.ChatUserBeans;
  6. import java.security.Timestamp;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author user
  18. */
  19. public class chat_user_model extends DBConnection {
  20.  
  21. public static void add_User(ChatUserBeans user ) throws ClassNotFoundException {
  22.  
  23. try {
  24.  
  25. Connection conn = createConnection();
  26. PreparedStatement s;
  27. s = conn.prepareStatement (
  28. "INSERT INTO chat_users (name, gravatar) VALUES(?,?)");
  29. s.setString (1,user.getuserName());
  30. s.setString (2, user.getgravatar());
  31. int count = s.executeUpdate ();
  32. s.close ();
  33. conn.close();
  34. //System.out.println (count + " rows were inserted");
  35.  
  36. } catch (SQLException ex) {
  37. Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
  38. }
  39.  
  40. }
  41. public static ChatUserBeans find_user(String name ) {
  42. ChatUserBeans user=new ChatUserBeans();
  43. try {
  44. Connection con = createConnection();
  45. PreparedStatement stmt = con.prepareStatement("select * from chat_users where name=? ");
  46. stmt.setString(1, name);
  47. ResultSet result= stmt.executeQuery();
  48. while(result.next())
  49. {
  50. user.setuserName(result.getString("name"));
  51. user.setgravatar(result.getString("gravatar"));
  52. }
  53. stmt.close();
  54. stmt.close();
  55. con.close();
  56. } catch (SQLException ex) {
  57. Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
  58. }
  59. return user ;
  60.  
  61. }
  62. public static void add_line(ChatLineBeans line ) throws ClassNotFoundException {
  63.  
  64. try {
  65.  
  66. Connection conn = createConnection();
  67. PreparedStatement s;
  68. s = conn.prepareStatement (
  69. "INSERT INTO chat_lins (aurther, gravatar,text) VALUES(?,?,?)");
  70. s.setString (1,line.getauther());
  71. s.setString (2, line.getgravatar());
  72. s.setString (3, line.gettext());
  73. int count = s.executeUpdate ();
  74. s.close ();
  75. conn.close();
  76.  
  77. //System.out.println (count + " rows were inserted");
  78.  
  79. } catch (SQLException ex) {
  80. Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
  81. }
  82.  
  83.  
  84. }
  85.  
  86. public static void delete_User(String name ) throws ClassNotFoundException {
  87.  
  88. try {
  89. Connection conn = createConnection();
  90. PreparedStatement s;
  91. s = conn.prepareStatement (
  92. "DLETE FROM chat_users WHERE name=?");
  93. s.setString (1,name);
  94. int count = s.executeUpdate ();
  95. s.close ();
  96. conn.close();
  97. } catch (SQLException ex) {
  98. Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
  99. }
  100.  
  101.  
  102.  
  103. }
  104. }
  105.  
  106. package model;
  107.  
  108.  
  109. import java.sql.Connection;
  110. import java.sql.DriverManager;
  111. import java.sql.SQLException;
  112. import java.util.logging.Level;
  113. import java.util.logging.Logger;
  114.  
  115. public class DBConnection {
  116. private static String hostName = "localhost";
  117. private static String port = "3306";
  118. private static String dbName ="120623085702";
  119. private static String userName = "root";
  120. private static String password="";
  121. public static Connection createConnection(){
  122. try {
  123. Class.forName("com.mysql.jdbc.Driver");
  124. String url = "jdbc:mysql://" + hostName + ":" + port + "/" + dbName;
  125. System.out.println(url);
  126. Connection con = DriverManager.getConnection(url,userName,password);
  127. return con;
  128.  
  129. } catch (SQLException ex) {
  130. Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  131. } catch (ClassNotFoundException ex) {
  132. Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  133. }
  134.  
  135. return null;
  136. }
  137.  
  138. }
Add Comment
Please, Sign In to add comment