-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
92 lines (74 loc) · 2.17 KB
/
Main.java
File metadata and controls
92 lines (74 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main;
import java.sql.*;
public class Main {
public static void main(String args[])
{
//database configuration parameters
String url = "jdbc:mysql://localhost:3306/";
String dbName = "mysql_test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "backbase";
try
{
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName, userName, password);
//print this if connection to the database was successful;
System.out.println("Connection was made successfully");
Statement st = conn.createStatement();
//insert a value here
//values to insert
int value_phone = 12345;
String value_name = "Jan3";
try
{
st.executeUpdate("INSERT INTO sql_table VALUES ('" + value_phone + "','" + value_name + "');");
//notify if insert was successful
System.out.println("Value successfully inserted into table");
}
catch(Exception e)
{
e.printStackTrace();
//notify if not successful
System.out.println("Could not insert value into table");
}
//call a value from the database
try
{
ResultSet res = st.executeQuery("SELECT * FROM sql_table;");
while(res.next()) {
int phone = res.getInt("phone_number");
String name = res.getString("name");
//notify if successful
System.out.println("Succesfully retrieved value from database");
System.out.println("Phone retrieved: " + phone + " name retrieved: " + name);
}
}
catch(Exception e)
{
e.printStackTrace();
//notify if unsuccessful
System.out.println("Could not retrieve the value from the Database");
}
//close connection here
try
{
conn.close();
//notify closed connection
System.out.println("Connection to database was closed");
}
catch(Exception e)
{
e.printStackTrace();
//notify unable to close the connection
System.out.println("Connection to the database was closed");
}
}
catch(Exception e)
{
e.printStackTrace();
//print to the console if connection was unsuccessful
System.out.println("Conncetion Failed...");
}
}
}