-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC
More file actions
79 lines (61 loc) · 2.1 KB
/
JDBC
File metadata and controls
79 lines (61 loc) · 2.1 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
"Program to implement JDBC in Java"
import java.sql.*;
import java.util.*;
class First_JDBC
{
public static void main(String a[])
{
//Creating the connection
String url = "jdbc:mysql://sqldatabase.cecptglr57pt.us-east-1.rds.amazonaws.com:3306/firstdb";
String user = "admin";
String pass = "admin12345";
Connection con=null;
Statement st=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
//Reference to connection interface
con = DriverManager.getConnection(url,user,pass);
System.out.println("connection successfull!");
st = con.createStatement();
//create table query
String sql = "CREATE TABLE EMPLOYEE " +
"(id INTEGER not NULL, " +
" Name VARCHAR(255), " +
" Age INTEGER, " +
" DEPT VARCHAR(255), " +
" SALARY INTEGER, " +
" PRIMARY KEY ( id ))";
System.out.println("creating table in a database...");
st.executeUpdate(sql);
//insering data into table
System.out.println("insering data into table");
sql = "INSERT INTO EMPLOYEE " +
"VALUES (1, 'Sam',25,'Sales',80000)";
st.executeUpdate(sql);
sql = "INSERT INTO EMPLOYEE " +
"VALUES (2, 'emily',24,'Management',850000)";
st.executeUpdate(sql);
sql = "INSERT INTO EMPLOYEE " +
"VALUES (3, 'joe',22,'IT',780000)";
st.executeUpdate(sql);
sql = "INSERT INTO EMPLOYEE " +
"VALUES (4, 'doe',20,'Logistics',700000)";
st.executeUpdate(sql);
sql = "INSERT INTO EMPLOYEE " +
"VALUES (5, 'nina',26,'Public Relations',750000)";
st.executeUpdate(sql);
System.out.println("closing connection...");
con.close();
}catch(SQLException se){
se.printStackTrace();
}
catch(Exception ex)
{
System.err.println(ex);
System.out.println("connection error");
}
finally{
}
}
}