Friday, November 22, 2019

MySQL connection with Jdbc Java 12 - 13 and insert in to table

First download the driver, then make a tables and download the xammp for the mysql server 5.7.

Video links:

Video link Basic: 

Source codes:
mysql> SHOW DATABASES;

mysql> CREATE DATABASE employees;

mysql> USE employees;


CREATE TABLE IF NOT EXISTS emp (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    age TINYINT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)  ENGINE=INNODB;


mysql> SELECT * FROM emp;

mysql> DESCRIBE emp;

mysql> SHOW TABLES;

mysql> INSERT INTO emp(name, age) VALUES('Fahad', 18);


mysql> exit




Java Codes:
package com.microtech;
import java.sql.*;

public class AnotherTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
try { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees?serverTimezone=UTC", "root", ""); PreparedStatement st = conn.prepareStatement("INSERT INTO emp(name, age) VALUES(?, ?);"); st.setString(1, "Junayed"); st.setShort(2, (short) 12); boolean resultSet = st.execute(); if (!resultSet) System.out.println("Data inserted successfully!"); else System.out.println("Try again!"); } catch(Exception e) { System.out.println("My exception: " + e); } System.out.print("End of the execution!");

}
}

No comments:

Post a Comment