14th Februrary 2019, Sandesh Shrestha
This program is about the CRUD operation via java's JDBC. This program can insert the user inputted data into our specified database.
Output
-- phpMyAdmin SQL Dump | |
-- version 4.7.4 | |
-- https://www.phpmyadmin.net/ | |
-- | |
-- Host: 127.0.0.1 | |
-- Generation Time: Feb 14, 2019 at 06:11 PM | |
-- Server version: 10.1.30-MariaDB | |
-- PHP Version: 7.2.1 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET AUTOCOMMIT = 0; | |
START TRANSACTION; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `book_registration` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `books_table` | |
-- | |
CREATE TABLE `books_table` ( | |
`id` int(11) NOT NULL, | |
`book_name` varchar(200) NOT NULL, | |
`author_name` varchar(200) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Dumping data for table `books_table` | |
-- | |
INSERT INTO `books_table` (`id`, `book_name`, `author_name`) VALUES | |
(1, 'java', 'Nishal sir'), | |
(10, 'Enter the name of book', 'Enter the name of author'), | |
(11, 'mathematics', 'sandesh'), | |
(12, 'mathematics', 'sandesh'); | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `books_table` | |
-- | |
ALTER TABLE `books_table` | |
ADD PRIMARY KEY (`id`); | |
-- | |
-- AUTO_INCREMENT for dumped tables | |
-- | |
-- | |
-- AUTO_INCREMENT for table `books_table` | |
-- | |
ALTER TABLE `books_table` | |
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13; | |
COMMIT; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
package com.gces.jdbc; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.sql.*; | |
public class Demo extends Frame | |
{ | |
// DECLARATIONS FOR DATABASE PART | |
public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; | |
public static final String DB_URL = "jdbc:mysql://localhost:3306/book_registration"; | |
public static final String UNAME = "root"; | |
public static final String PASSWORD = ""; | |
String insertQuery; | |
// for GUI PART | |
Label bookNameLabel, authorNameLabel; | |
TextField bookTextField, authorTextField; | |
Button saveButton; | |
Demo() | |
{ | |
prepareGUI(); | |
} | |
// main function | |
public static void main(String[] args) | |
{ | |
Demo obj = new Demo(); | |
obj.dataBaseFunction(); | |
} | |
public void prepareGUI() | |
{ | |
this.setTitle("Book Registration System"); | |
this.setSize(500,500); | |
this.setVisible(true); | |
this.setLayout(new FlowLayout()); | |
// closable frame | |
this.addWindowListener(new WindowAdapter() | |
{ | |
@Override public void windowClosing(WindowEvent we) | |
{ | |
System.exit(0); | |
} | |
}); | |
bookNameLabel = new Label("Book:"); | |
bookTextField = new TextField("Enter the name of Book",50); | |
authorNameLabel = new Label("Author:"); | |
authorTextField = new TextField("Enter the name of author",50); | |
saveButton = new Button("SAVE"); | |
this.add(bookNameLabel); | |
this.add(bookTextField); | |
this.add(authorNameLabel); | |
this.add(authorTextField); | |
this.add(saveButton); | |
// when user focus on textFields the initial text should be gone. This is for Keyboard focus | |
bookTextField.addFocusListener(new FocusListener() | |
{ | |
public void focusGained(FocusEvent f) | |
{ | |
bookTextField.setText(""); | |
} | |
public void focusLost(FocusEvent f) | |
{ | |
if(bookTextField.getText().equals("")) | |
bookTextField.setText("Enter the name of book"); | |
} | |
}); | |
authorTextField.addFocusListener(new FocusListener() | |
{ | |
public void focusGained(FocusEvent f) | |
{ | |
authorTextField.setText(""); | |
} | |
public void focusLost(FocusEvent f) | |
{ | |
if(authorTextField.getText().equals("")) | |
authorTextField.setText("Enter the name of author"); | |
} | |
}); | |
} | |
//database roles comes when user enter the data and click save button | |
public void dataBaseFunction() | |
{ | |
saveButton.addActionListener(new ActionListener() | |
{ | |
@Override public void actionPerformed(ActionEvent e) | |
{ | |
try | |
{ | |
insertQuery = "INSERT INTO `books_table`(`book_name`,`author_name`)"+"VALUES(?,?)"; | |
Class.forName(JDBC_DRIVER); | |
Connection con = DriverManager.getConnection(DB_URL,UNAME,PASSWORD); | |
PreparedStatement pst = con.prepareStatement(insertQuery); | |
pst.setString(1,bookTextField.getText()); | |
pst.setString(2,authorTextField.getText()); | |
pst.executeUpdate(); | |
pst.close(); | |
con.close(); | |
} | |
catch (ClassNotFoundException e1) { | |
e1.printStackTrace(); | |
} catch (SQLException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
}); | |
} | |
} |