Guest User

Untitled

a guest
Jan 31st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. //Mysql connection
  2. //Author: Milon
  3.  
  4. //import java.sql.*;
  5. import java.sql.Connection;
  6. import java.sql.Statement;
  7. import java.sql.PreparedStatement;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11.  
  12. public class MysqlConnection{
  13. private static String driver = "com.mysql.jdbc.Driver";
  14. private static String connection = "jdbc:mysql://localhost:3306/milon"; //'milon' is your database name
  15. private static String user = "root"; //'root' is username
  16. private static String password = "pass"; //'pass' is password
  17.  
  18.  
  19. private static Connection con = null;
  20. private static Statement state = null;
  21. private static ResultSet result;
  22. private static PreparedStatement pstate;
  23.  
  24. public static void main(String args[])/* throws Exception*/{
  25. mysqlConnect();
  26. insertData("s6s","milon","dgh","ghjgh");
  27. deleteData("sd");
  28. countRow("dictionary");
  29. updateData("ss", "suru");
  30. showData("surayea");
  31. closeConnection();
  32. }
  33.  
  34.  
  35. public static void mysqlConnect(){
  36. try{
  37. Class.forName(driver);
  38. con = DriverManager.getConnection(connection, user, password);
  39. System.out.println("Successfully connected to database.");
  40. }
  41. catch(ClassNotFoundException e){
  42. System.err.println("Couldn't load driver.");
  43. }
  44. catch(SQLException e){
  45. System.err.println("Couldn't connect to database.");
  46. }
  47. }
  48.  
  49.  
  50. public static void closeConnection(){
  51. try{
  52. if(!con.isClosed()){
  53. con.close();
  54. System.out.println("Database closed successfully.");
  55. }
  56. }
  57. catch(NullPointerException e){
  58. System.err.println("Couldn't load driver.");
  59. }
  60. catch(SQLException e){
  61. System.err.println("Couldn't close database.");
  62. }
  63. }
  64.  
  65. public static void insertData(String word, String meaning, String synonyms, String antonyms){
  66. try{
  67. //using PreparedStatement
  68. pstate = con.prepareStatement("insert into dictionary(word, meaning, synonyms, antonyms)"+
  69. "values(?,?,?,?)");
  70. pstate.setString(1, word);
  71. pstate.setString(2, meaning);
  72. pstate.setString(3, synonyms);
  73. pstate.setString(4, antonyms);
  74. int value = pstate.executeUpdate();
  75.  
  76. //using Statement
  77. //state = con.createStatement();
  78. //int value = state.executeUpdate("insert into dictionary(word, meaning, synonyms, antonyms)"+
  79. // "values('"+word+"', '"+meaning+"', '"+synonyms+"', '"+antonyms+"')");
  80.  
  81. System.out.println("Query OK, 1 row insertedted.");
  82. }
  83. catch(SQLException e){
  84. System.err.println("Query error.");
  85. }
  86. }
  87.  
  88. public static void deleteData(String word){
  89. try{
  90. //using PreparedStatement
  91. pstate = con.prepareStatement("delete from dictionary where word = ?");
  92. pstate.setString(1,"word");
  93. int value = pstate.executeUpdate();
  94.  
  95. //using Statement
  96. //state = con.createStatement();
  97. //int value = state.executeUpdate("delete from dictionary where word='"+word+"'");
  98.  
  99. System.out.println("Query OK, 1 row deleted.");
  100. }
  101. catch(SQLException e){
  102. System.err.println("Query error.");
  103. }
  104. }
  105.  
  106. public static void countRow(String table){
  107. try{
  108. result = state.executeQuery("SELECT COUNT(*) FROM "+table);
  109. result.next();
  110. int rowcount = result.getInt(1);
  111. System.out.println("Number of rows: "+rowcount);
  112. }
  113. catch(SQLException e){
  114. System.err.println("Query error.");
  115. }
  116. }
  117.  
  118. public static void showData(String word){
  119. try{
  120. state = con.createStatement();
  121. result = state.executeQuery("select * from dictionary where word='"+word+"'");
  122. while(result.next()){
  123. String word1 = result.getString("word");
  124. String mean = result.getString("meaning");
  125. String syno = result.getString("synonyms");
  126. String anto = result.getString("antonyms");
  127. System.out.println("Word: "+word1+" Meaning: "+mean+" Synonyms: "+syno+" Antonyms: "+anto);
  128. }
  129. }
  130. catch(SQLException e){
  131. System.err.println("Query error.");
  132. }
  133. catch(NullPointerException e){
  134. System.err.println("Element not found.");
  135. }
  136. }
  137.  
  138. public static void updateData(String word, String meaning){
  139. try{
  140. //using Statement
  141. //state = con.createStatement();
  142. //int value = state.executeUpdate("update dictionary set meaning='"+meaning+"' where word='"+word+"'");
  143.  
  144. //using PreparedStatement
  145. pstate = con.prepareStatement("update dictionary set meaning= ? whrere word = ?");
  146. pstate.setString(1, meaning);
  147. pstate.setString(2, word);
  148. pstate.executeUpdate();
  149.  
  150. System.out.println("Query OK, 1 row updated.");
  151. }
  152. catch(SQLException e){
  153. System.err.println("Query error."+e.getMessage());
  154. }
  155. }
  156.  
  157. }
Add Comment
Please, Sign In to add comment