-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.java
More file actions
51 lines (43 loc) · 1.11 KB
/
database.java
File metadata and controls
51 lines (43 loc) · 1.11 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
package exercise;
import java.sql.*;
public class database {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/";
static final String USER = "demo";
static final String PWD = "Rishita@22";
public static void main(String[] args) {
Connection con = null;
Statement statement = null;
try {
//Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("connecting to database...");
con=DriverManager.getConnection(DB_URL, USER, PWD);
// Execute query
System.out.println("Creating database...");
statement = con.createStatement();
String sql = "create database student";
boolean result = statement.execute(sql);
System.out.println(result);
System.out.println("Database created...");
}catch (SQLException e) {
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
try {
if(statement!=null) {
statement.close();
}
if(con!=null) {
con.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}