-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLConnectivity.java
More file actions
30 lines (25 loc) · 963 Bytes
/
Copy pathSQLConnectivity.java
File metadata and controls
30 lines (25 loc) · 963 Bytes
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
import java.sql.*;
public class SQLConnectivity {
public static void main(String[] args) throws Exception {
// 1. Load MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. Connect to the database
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/DBMS",
"root",
"YashPujara@18"
);
// 3. Create a Statement
Statement stmt = conn.createStatement();
// 4. Create table if it doesn't exist
String createTableSQL = "Select * from Work";
ResultSet rs=stmt.executeQuery(createTableSQL);
System.out.println("Name: \t Company Name \t Salary\n");
while(rs.next())
{
System.out.println(rs.getString(1)+"\t "+rs.getString(2)+"\t \t "+rs.getInt(3));
}
//closing the connection
conn.close();
}
}