0% found this document useful (0 votes)
21 views

File - New Type Demojdbc

This document outlines 7 steps to insert data into a MySQL database using JDBC in Java. It involves importing necessary JDBC libraries, getting a connection to the database, creating a statement object to execute an INSERT query, catching any exceptions, and closing the statement and connection objects. The code sample demonstrates inserting a record into an EMPLOYEE table with 4 columns.

Uploaded by

kassahun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

File - New Type Demojdbc

This document outlines 7 steps to insert data into a MySQL database using JDBC in Java. It involves importing necessary JDBC libraries, getting a connection to the database, creating a statement object to execute an INSERT query, catching any exceptions, and closing the statement and connection objects. The code sample demonstrates inserting a record into an EMPLOYEE table with 4 columns.

Uploaded by

kassahun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

File –new type demoJDBC

2.

3. Follow 7 steps:
package com.howtodoinjava.jdbc.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class InsertDataDemo {


public static void main(String[] args) {
Connection connection = null;
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password");

stmt = connection.createStatement();
stmt.execute("INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME,STAT_CD) "
+ "VALUES (1,'Lokesh','Gupta',5)");
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
stmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

You might also like