· 10 years ago · Apr 15, 2016, 07:18 AM
1package com.company;
2
3import java.sql.*;
4
5public class Main {
6
7 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //Configure the driver needed
8 static final String DB_CONNECTION_URL = "jdbc:mysql://localhost:3306/vet"; //Connection string – where's the database?
9 static final String USER = "clara"; //TODO replace with your username
10 static final String PASSWORD = "password"; //TODO replace with your password
11
12 public static void main(String[] args) {
13
14 try {
15
16 Class.forName(JDBC_DRIVER);
17
18 } catch (ClassNotFoundException cnfe) {
19 System.out.println("Can't instantiate driver class; check you have drives and classpath configured correctly?");
20 cnfe.printStackTrace();
21 System.exit(-1); //No driver? Need to fix before anything else will work. So quit the program
22 }
23
24 Statement statement = null;
25 Connection conn = null;
26
27 /** New code here **/
28
29 ResultSet rs = null;
30
31 /** End of new code **/
32
33 try {
34
35 //You should have already created a database via terminal/command prompt OR MySQL Workbench
36
37 conn = DriverManager.getConnection(DB_CONNECTION_URL, USER, PASSWORD);
38 statement = conn.createStatement();
39
40 //Create a table in the database, if it does not exist already
41 String createTableSQL = "CREATE TABLE if not exists Dogs (Name varchar(30), Age int)";
42 statement.executeUpdate(createTableSQL);
43 System.out.println("Created Dogs table");
44
45 //Add some data
46 String addDataSQL = "INSERT INTO Dogs VALUES ('Poppy', 3)";
47 statement.executeUpdate(addDataSQL);
48
49 addDataSQL = "INSERT INTO Dogs VALUES ('Zelda', 5)";
50 statement.executeUpdate(addDataSQL);
51 System.out.println("Added two rows of data");
52
53 /** More new code here **/
54
55 addDataSQL = "INSERT INTO Dogs VALUES ('Fred', 7)";
56 statement.executeUpdate(addDataSQL);
57
58 addDataSQL = "INSERT INTO Dogs VALUES ('Lassie', 12)";
59 statement.executeUpdate(addDataSQL);
60
61 String fetchAllDataSQL = "SELECT * FROM Dogs";
62 rs = statement.executeQuery(fetchAllDataSQL);
63 while (rs.next()) {
64 String dogName = rs.getString("Name");
65 int dogAge = rs.getInt("Age");
66 System.out.println("Dog name = " + dogName + " dog age = " + dogAge);
67 }
68
69 /** End of new code here **/
70
71
72 } catch (SQLException se) {
73 se.printStackTrace();
74 } catch (Exception e) {
75 e.printStackTrace();
76 } finally {
77 //A finally block runs whether an exception is thrown or not. Close resources and tidy up whether this code worked or not.
78 try {
79 if (statement != null) {
80 statement.close();
81 System.out.println("Statement closed");
82 }
83 } catch (SQLException se){
84 //Closing the connection could throw an exception too
85 se.printStackTrace();
86 }
87 try {
88 if (conn != null) {
89 conn.close(); //Close connection to database
90 System.out.println("Database connection closed");
91 }
92 } catch (SQLException se) {
93 se.printStackTrace();
94 }
95 }
96 System.out.println("End of program");
97 }
98}