Guest User

Untitled

a guest
May 9th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. import java.sql.*;
  2. import java.sql.Connection;
  3.  
  4.  
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9.  
  10. /**
  11. * This class demonstrates how to connect to MySQL and run some basic commands.
  12. *
  13. * In order to use this, you have to download the Connector/J driver and add
  14. * its .jar file to your build path. You can find it here:
  15. *
  16. * http://dev.mysql.com/downloads/connector/j/
  17. *
  18. * You will see the following exception if it's not in your class path:
  19. *
  20. * java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
  21. *
  22. * To add it to your class path:
  23. * 1. Right click on your project
  24. * 2. Go to Build Path -> Add External Archives...
  25. * 3. Select the file mysql-connector-java-5.1.24-bin.jar
  26. * NOTE: If you have a different version of the .jar file, the name may be
  27. * a little different.
  28. *
  29. * The user name and password are both "root", which should be correct if you followed
  30. * the advice in the MySQL tutorial. If you want to use different credentials, you can
  31. * change them below.
  32. *
  33. * You will get the following exception if the credentials are wrong:
  34. *
  35. * java.sql.SQLException: Access denied for user 'userName'@'localhost' (using password: YES)
  36. *
  37. * You will instead get the following exception if MySQL isn't installed, isn't
  38. * running, or if your serverName or portNumber are wrong:
  39. *
  40. * java.net.ConnectException: Connection refused
  41. */
  42. public class DBDemoo {
  43.  
  44. /** The name of the MySQL account to use (or empty for anonymous) */
  45. private final String userName = "root";
  46.  
  47. /** The password for the MySQL account (or empty for anonymous) */
  48. private final String password = "root";
  49.  
  50. /** The name of the computer running MySQL */
  51. private final String serverName = "localhost";
  52.  
  53. /** The port of the MySQL server (default is 3306) */
  54. private final int portNumber = 3306;
  55.  
  56. /** The name of the database we are testing with (this default is installed with MySQL) */
  57. private final String dbName = "test";
  58.  
  59. /** The name of the table we are testing with */
  60. private final String tableName = "JDBC_TESTTT";
  61.  
  62. /**
  63. * Get a new database connection
  64. *
  65. * @return
  66. * @throws SQLException
  67. */
  68. public Connection getConnection() throws SQLException {
  69. Connection conn = null;
  70. Properties connectionProps = new Properties();
  71. connectionProps.put("user", this.userName);
  72. connectionProps.put("password", this.password);
  73.  
  74. conn = DriverManager.getConnection("jdbc:mysql://"
  75. + this.serverName + ":" + this.portNumber + "/" + this.dbName,
  76. connectionProps);
  77.  
  78. return conn;
  79. }
  80.  
  81. /**
  82. * Run a SQL command which does not return a recordset:
  83. * CREATE/INSERT/UPDATE/DELETE/DROP/etc.
  84. *
  85. * @throws SQLException If something goes wrong
  86. */
  87. public boolean executeUpdate(Connection conn, String command) throws SQLException {
  88. Statement stmt = null;
  89. try {
  90. stmt = conn.createStatement();
  91. stmt.executeUpdate(command); // This will throw a SQLException if it fails
  92. return true;
  93. } finally {
  94.  
  95. // This will run whether we throw an exception or not
  96. if (stmt != null) { stmt.close(); }
  97. }
  98. }
  99.  
  100. /**
  101. * Connect to MySQL and do some stuff.
  102. */
  103. public void run() {
  104.  
  105. // Connect to MySQL
  106. Connection conn = null;
  107. Statement stmt = null;
  108. try {
  109. conn = this.getConnection();
  110. System.out.println("Connected to database");
  111. } catch (SQLException e) {
  112. System.out.println("ERROR: Could not connect to the database");
  113. e.printStackTrace();
  114. return;
  115. }
  116.  
  117. // Create a table
  118. try {
  119. String createString =
  120. "CREATE TABLE " + this.tableName + " ( " +
  121. "ID INTEGER NOT NULL, " +
  122. "NAME varchar(40) NOT NULL, " +
  123. "STREET varchar(40) NOT NULL, " +
  124. "CITY varchar(20) NOT NULL, " +
  125. "STATE char(2) NOT NULL, " +
  126. "ZIP char(5), " +
  127. "PRIMARY KEY (ID))";
  128. this.executeUpdate(conn, createString);
  129. System.out.println("Created a table");
  130. } catch (SQLException e) {
  131. System.out.println("ERROR: Could not create the table");
  132. e.printStackTrace();
  133. return;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. //Insert into the table
  140. try {
  141. /* String insertString =
  142. "INSERT INTO " + this.tableName + " ( " +
  143. "ID INTEGER NOT NULL, " +
  144. "NAME varchar(40) NOT NULL, " +
  145. "STREET varchar(40) NOT NULL, " +
  146. "CITY varchar(20) NOT NULL, " +
  147. "STATE char(2) NOT NULL, " +
  148. "ZIP char(5), " +
  149. "PRIMARY KEY (ID))";*/
  150. System.out.println("Inserting tables to the records");
  151. stmt = conn.createStatement();
  152.  
  153. String insertString = "INSERT INTO JDBC_TESTT VALUES (1002, 'Hey', 'shsh', 'shshsh','ss','123')";
  154.  
  155. stmt.executeUpdate(insertString);
  156.  
  157.  
  158. System.out.println("Table Inserted");
  159. } catch (SQLException e) {
  160. System.out.println("ERROR: Could not Insert to the table");
  161. e.printStackTrace();
  162. return;
  163. }
  164.  
  165. //Extracting Data from the table
  166.  
  167. System.out.println("Creating statement..");
  168. try
  169. {
  170.  
  171. stmt = conn.createStatement();
  172. String retreive;
  173. retreive = "Select name from JDBC_TESTT";
  174. ResultSet rs = stmt.executeQuery(retreive);
  175.  
  176. while(rs.next()) {
  177. String name = rs.getString("name");
  178.  
  179.  
  180. System.out.println("Name:"+name);
  181.  
  182.  
  183. }
  184. }
  185. catch(SQLException se){
  186. //Handle errors for JDBC
  187. se.printStackTrace();
  188. }
  189.  
  190.  
  191. // Drop the table
  192. //try {
  193. // String dropString = "DROP TABLE " + this.tableName;
  194. // this.executeUpdate(conn, dropString);
  195. // System.out.println("Dropped the table");
  196. // } catch (SQLException e) {
  197. // System.out.println("ERROR: Could not drop the table");
  198. // e.printStackTrace();
  199. // return;
  200. //}
  201. }
  202.  
  203. /**
  204. * Connect to the DB and do some stuff
  205. */
  206. public static void main(String[] args) {
  207. DBDemoo app = new DBDemoo();
  208. app.run();
  209. }
  210. }
Add Comment
Please, Sign In to add comment