-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJDBCCreateTable.java
54 lines (51 loc) · 1.51 KB
/
JDBCCreateTable.java
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
package dbms;
import java.sql.*;
public class JDBCCreateTable {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB = "jdbc:mysql://localhost:3306/student";
// 1. Database User name & Password
static final String USER = "admin";
static final String PWD = "sjcet123";
public static void main(String[] args) {
Connection con = null;
Statement statement = null;
try {
// 2. Register JDBC driver
Class.forName(JDBC_DRIVER);
// 3. Open a connection
System.out.println("Connecting to database...");
con = DriverManager.getConnection(DB, USER,PWD);
// 4. Execute a query
System.out.println("Creating Table..");
statement = con.createStatement();
String sql = "create table user " +
"(roll_number integer not null, " +
" first_name varchar(50), " +
" last_name varchar(50), " +
" age integer, " +
" primary key ( roll_number ))";
int i = statement.executeUpdate(sql);
System.out.println("Table Created,"+"Value of i="+i);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
try {
System.out.println("Closing Connection");
if (statement != null)
statement.close();
if (con != null)
con.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
System.out.println("Ending Program..");
}
}