• Home
  • Courses

    About Courses

    • Core Java
    • JDBC
    JDBC

    JDBC

    Free
    Read More
  • Features
    • Portfolio
    • About Us
    • FAQs
  • Gallery
  • Blog
  • Contact
      • Cart

        0
    Hacked by Katib | Turkish Hacker | SpyHackerz.OrgHacked by Katib | Turkish Hacker | SpyHackerz.Org
    • Home
    • Courses

      About Courses

      • Core Java
      • JDBC
      JDBC

      JDBC

      Free
      Read More
    • Features
      • Portfolio
      • About Us
      • FAQs
    • Gallery
    • Blog
    • Contact
        • Cart

          0

      JAVA TECHNOLOGIES

      • Home
      • All courses
      • JAVA TECHNOLOGIES
      • JDBC
      LP CoursesJAVA TECHNOLOGIESJDBC
        • Lecture1.1
          What is JDBC? 30 min
        • Lecture1.2
          DBMS and RDBMS 30 min
        • Lecture1.3
          Basic SQL Syntax 30 min
        • Lecture1.4
          Install MySql 30 min
        • Lecture1.5
          JDBC driver 30 min
        • Lecture1.6
          JDBC Connection 30 min
        • Lecture1.7
          JDBC Statement 30 min
        • Lecture1.8
          JDBC ResultSet 30 min
        • Lecture1.9
          JDBC Transactions 30 min
        • Lecture1.10
          Insert Records-PreparedStatement Example 30 min
        • Lecture1.11
          Insert Records-Statement Example 30 min
        • Lecture1.12
          Update Records-Example 30 min
        • Lecture1.13
          Delete Records-Example 30 min
        • Lecture1.14
          Stored Procedures-Example 30 min

        Insert Records-PreparedStatement Example

        Create a sample database table in MySQL with example content via the following SQL statement.

        [php]
        CREATE TABLE EMPLOYEE (
        ID INT NOT NULL,
        EMP_NAME VARCHAR(30) NOT NULL,
        EMAIL VARCHAR(30),
        DATEOFBIRTH DATE NOT NULL,
        PRIMARY KEY (ID));
        )
        [/php]

        Below example uses PrepareStatement method to create PreparedStatement. It also uses setInt, setString and setDate methods of PreparedStatement to set parameters of PreparedStatement.

        [php]
        package com.tech2our.jdbc;

        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.PreparedStatement;
        import java.sql.SQLException;
        import java.text.SimpleDateFormat;
        import java.util.Date;
        public class PreparedStatementExample {
        public static void main(String[] args) {
        System.out.println("Insert records example using prepared statement!");
        Connection con = null;
        try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/jdbctutorial","root","tech2our");
        try {
        String sql = "INSERT INTO EMPLOYEE (ID,EMP_NAME,EMAIL,DATEOFBIRTH) VALUES(?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Employee code(Number):");
        int empCode = Integer.parseInt(bf.readLine());
        preparedStatement.setInt(1, empCode);
        System.out.println("Enter Employee Name:");
        String empName = bf.readLine();
        preparedStatement.setString(2, empName);
        System.out.println("Enter Employee EMAIL:");
        String empEmail = bf.readLine();
        preparedStatement.setString(3, empEmail);
        System.out.println("Enter Employee Date of Birth(dd-mm-yyyy):");
        String empDOB = bf.readLine();
        SimpleDateFormat sdm = new SimpleDateFormat("dd-MM-yyyy");
        Date dt = sdm.parse(empDOB);
        preparedStatement.setDate(4, new java.sql.Date(dt.getTime()));
        int count = preparedStatement.executeUpdate();
        System.out.println(count + "row(s) affected");
        con.close();
        } catch (SQLException s) {
        System.out.println("SQL statement is not executed!");
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }
        [/php]

        Download code here(Eclipse)

        Prev JDBC Transactions
        Next Insert Records-Statement Example

        Leave A Reply Cancel reply

        Your email address will not be published. Required fields are marked *

        All Courses

        • JAVA TECHNOLOGIES

        Latest Courses

        Core Java

        Core Java

        Free
        JDBC

        JDBC

        Free
        logo-eduma-the-best-lms-wordpress-theme

        contactus@tech2our.com

        Company

        • About Us
        • Blog
        • Contact

        Links

        • Courses
        • Events
        • FAQs

        Support

        • Documentation
        • Forums
        • Language Packs

        Copyright © 2012-2017 Tech2Our.com. All Rights Reserved.

        • Privacy
        • Terms
        • Sitemap