· 9 years ago · Oct 26, 2016, 05:26 AM
1import java.sql.*;
2
3public class ProjectTest {
4
5 public static void main(String[] args) throws SQLException{
6 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
7 Connection con = DriverManager.getConnection(
8 "jdbc:oracle:thin:@localhost:1522:ug", "ora_q3l8", "a29240116");
9 Statement stmt = con.createStatement();
10
11 String dropSQL = "DROP TABLE children ";
12
13 String createSQL = "CREATE TABLE children " +
14 "(ChildID INTEGER not NULL, " +
15 " ChildName VARCHAR(30), " +
16 " ChildAddress VARCHAR(30), " +
17 " PRIMARY KEY ( ChildID ))";
18
19 String insertSQL = "insert into children values (17683634, 'Susan Martin', '177 Green Street')";
20
21 String selectSQL = "SELECT * FROM children";
22
23 // ideally we'd check if the table exists before dropping it, but I can't figure out how to do that right now
24 stmt.executeUpdate(dropSQL);
25
26 // create the table and add a row
27 stmt.executeUpdate(createSQL);
28 stmt.executeUpdate(insertSQL);
29
30 ResultSet rs = stmt.executeQuery(selectSQL);
31
32 // print every name in the database: there should be one result, Susan Martin
33 while(rs.next())
34 {
35 System.out.println(rs.getString("ChildName"));
36 }
37 con.close();
38 }
39}