Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package abot;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5.  
  6.  
  7.  
  8. public class MysqlConnect{
  9. static String url = "jdbc:mysql://localhost:3306/";
  10. static String dbName = "abot";
  11. static String driver = "com.mysql.jdbc.Driver";
  12. static String userName = "root";
  13. static String password = "root";
  14. static String questionsTable = "questions";
  15. public static void createDatabase() {
  16. System.out.println("[A-bot]Attempting DataBase Creation...");
  17. Connection con = null;
  18. try{
  19. Class.forName(driver).newInstance();
  20. con = DriverManager.getConnection(url+"constant",userName,password);
  21. try{
  22. Statement st = con.createStatement();
  23. st.executeUpdate("CREATE DATABASE "+dbName);
  24. System.out.println("[A-bot]DataBase '" + dbName + "' created.");
  25.  
  26. }
  27. catch (SQLException s){
  28. System.out.println("[A-bot]DataBase creation aborted. Error:" + s);
  29. }
  30. }
  31. catch (Exception e){
  32. e.printStackTrace();
  33. }
  34. }
  35. public static void testConnection() {
  36. System.out.println("[A-Bot]Testing DataBase Connection...");
  37. Connection conn = null;
  38. try {
  39. Class.forName(driver).newInstance();
  40. conn = DriverManager.getConnection(url+dbName,userName,password);
  41. System.out.println("[A-Bot]DataBase Connection established.");
  42. conn.close();
  43. createQuestionsTable();
  44. createAnswersTable();
  45. System.out.println("[A-Bot]DataBase Connection test finished.");
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49.  
  50. }
  51. public static void createQuestionsTable() {
  52. System.out.println("[A-bot]Attempting to Create Question MYSQL Table...");
  53. Connection con = null;
  54. try{
  55. Class.forName(driver).newInstance();
  56. con = DriverManager.getConnection(url+dbName, userName, password);
  57. try{
  58. Statement st = con.createStatement();
  59. String table = "CREATE TABLE questions(id integer, questions varchar(100))";
  60. st.executeUpdate(table);
  61. System.out.println("[A-bot]Question Table successfully created.");
  62. }
  63. catch(SQLException s){
  64. System.out.println("[A-bot]Question table creation aborted. Error:" + s);
  65. }
  66. con.close();
  67. }
  68. catch (Exception e){
  69. e.printStackTrace();
  70. }
  71. }
  72. public static void createAnswersTable() {
  73. System.out.println("[A-bot]Attempting to Create Answer MYSQL Table...");
  74. Connection con = null;
  75. try{
  76. Class.forName(driver).newInstance();
  77. con = DriverManager.getConnection(url+dbName, userName, password);
  78. try{
  79. Statement st = con.createStatement();
  80. String table = "CREATE TABLE answerss(id integer, answers varchar(100))";
  81. st.executeUpdate(table);
  82. System.out.println("[A-bot]Answer Table successfully created.");
  83. }
  84. catch(SQLException a){
  85. System.out.println("[A-bot]Answer table creation aborted. Error:" + a);
  86. }
  87. con.close();
  88. }
  89. catch (Exception e){
  90. e.printStackTrace();
  91. }
  92. }
  93. public static void addQuestion(String Question) {
  94. System.out.println("[A-bot]Attempting to add Question '" + Question + "' to MYSQL");
  95. Connection con = null;
  96. try{
  97. Class.forName(driver);
  98. con = DriverManager.getConnection(url+dbName, userName, password);
  99. try{
  100.  
  101. Statement st = con.createStatement();
  102. int val = st.executeUpdate("INSERT questions VALUES("+getRowCountPlusOne(questionsTable)+","+"'"+ Question +"'"+")");
  103. System.out.println("[A-bot]Question '" + Question + "' added successfully to MYSQL.");
  104. }
  105. catch (SQLException s){
  106. System.out.println("[A-bot]Adding question aborted. Error:" + s);
  107. }
  108. }
  109. catch (Exception e){
  110. e.printStackTrace();
  111. }
  112. }
  113. public static int getRowCountPlusOne(String table) {
  114. System.out.println("[A-bot]Attempting to count rows in table '" + table + "'");
  115. Connection con = null;
  116. int count = 0;
  117. try{
  118. Class.forName(driver);
  119. con = DriverManager.getConnection(url+dbName, userName, password);
  120. try{
  121. Statement st = con.createStatement();
  122. ResultSet res = st.executeQuery("SELECT COUNT(*) FROM "+table);
  123. while (res.next()){
  124. count = res.getInt(1);
  125. }
  126. System.out.println("[A-bot]Rows in " + table + ":"+count);
  127. }
  128. catch (SQLException s){
  129. System.out.println("[A-bot]RowCount aborted. Error:" + s);
  130. }
  131. }
  132. catch (Exception e){
  133. e.printStackTrace();
  134. }
  135. return count+1;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement