|
| 1 | +// Demonstratting insertion of records |
| 2 | +import java.sql.Connection; |
| 3 | +import java.sql.DriverManager; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.sql.Statement; |
| 6 | + |
| 7 | +public class JDBCExample5 { |
| 8 | + static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT"; |
| 9 | + static final String USER = "guest"; |
| 10 | + static final String PASS = "guest123"; |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + // Open a connection |
| 14 | + try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); |
| 15 | + Statement stmt = conn.createStatement(); |
| 16 | + ) { |
| 17 | + // Execute a query |
| 18 | + System.out.println("Inserting records into the table..."); |
| 19 | + String sql = "INSERT INTO Registration VALUES (100, 'Zara', 'Ali', 18)"; |
| 20 | + stmt.executeUpdate(sql); |
| 21 | + sql = "INSERT INTO Registration VALUES (101, 'Mahnaz', 'Fatma', 25)"; |
| 22 | + stmt.executeUpdate(sql); |
| 23 | + sql = "INSERT INTO Registration VALUES (102, 'Zaid', 'Khan', 30)"; |
| 24 | + stmt.executeUpdate(sql); |
| 25 | + sql = "INSERT INTO Registration VALUES(103, 'Sumit', 'Mittal', 28)"; |
| 26 | + stmt.executeUpdate(sql); |
| 27 | + System.out.println("Inserted records into the table..."); |
| 28 | + } catch (SQLException e) { |
| 29 | + e.printStackTrace(); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
0 commit comments