Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel...
-
Upload
barrie-greene -
Category
Documents
-
view
252 -
download
10
Transcript of Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel...
![Page 1: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/1.jpg)
Java Swing Tutorial
![Page 2: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/2.jpg)
Java Swing
* A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc
![Page 3: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/3.jpg)
IntroductionsSwing is one of the Graphical User Interface tool. Swing is used to develop the graphical user interface (GUI) in java. Swing components used for the building of GUI. Swing components are helpful for interactivity to Java applications. The components of Swing toolkit are given below: list controls buttons labels tree controls table controls
![Page 4: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/4.jpg)
Java Swing Class Hierarchy
![Page 5: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/5.jpg)
Swing Events
event source // The Event source is the object. It generates Events. event object // The Event object encapsulates the condition changes in the event source.event listener // The Event listener is the object that requests to be notified Event source:Object is handling an event to the event listener.Event handling in Swing toolkit is very easy to handle and powerful.Objects are notified when a specific event occurs.
![Page 6: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/6.jpg)
JFrame
Open frame place the icon on the title bar.Methods are as follows:frame.setIconImage(Toolkit.getDefaultToolkit().getImage("icon_confused.gif")); it’s using the Image class method named getImage().frame.getDefaultToolkit(): //This is the method of the Toolkit class which gets the default toolkit.
![Page 7: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/7.jpg)
createAndShowGUI
private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Hi.."); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add a label. JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); }
![Page 8: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/8.jpg)
Menus
JMenuBar
JMenu
JMenuItem
![Page 9: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/9.jpg)
checkBox
import javax.swing.*; JCheckBox chk = new JCheckBox("This is the Check Box"); frame.add(chk);
![Page 10: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/10.jpg)
Text Areaimport javax.swing.JTextArea; // Create a text area with some initial text JTextArea textarea = new JTextArea("Initial Text"); int rows = 20; int cols = 30; textarea = new JTextArea("Initial Text", rows, cols);TextFieldJTextField t = new JTextField("Text field 3", 8);
![Page 11: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/11.jpg)
JButton
Button: Two types: JButton(), JButton(Name) JButton () is used for create blank JButton instance. JButton (Name) is used for create a JButton instance with the specified text. JButton Button1; Button1 = new JButton ("Black is White"); add (Button1);
![Page 12: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/12.jpg)
Layout Managers
Most Swing UIs utilise a LayoutManager to control positioning of itemsThere is a choice of these which work in different waysInitially we do without one, and position items ourselves: frame.setLayout(null);
![Page 13: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/13.jpg)
Absolute positioning
JFrame frame = new JFrame("I am a JFrame");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setBounds(20,30,300,100);frame.setLayout(null);JButton butt=new JButton("Click me");frame.getContentPane().add(butt);butt.setBounds(20, 20, 200,20);frame.setVisible(true);
![Page 14: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/14.jpg)
FlowLayout
JFrame.setDefaultLookAndFeelDecorated(true);JFrame frame = new JFrame("FlowLayout");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new FlowLayout());JButton b1 = new JButton("Hello");frame.getContentPane().add(b1);JButton b2 = new JButton("Two");frame.getContentPane().add(b2);JTextField t1 = new JTextField("Text here");frame.getContentPane().add(t1);frame.pack();frame.setVisible(true);
![Page 15: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/15.jpg)
BorderLayoutJFrame.setDefaultLookAndFeelDecorated(true);JFrame frame = new JFrame("Border");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JButton b1 = new JButton("At the top");frame.getContentPane().add(b1,BorderLayout.PAGE_START );JButton b2 = new JButton("Bottom");frame.getContentPane().add(b2,BorderLayout.PAGE_END);JTextField t1 = new JTextField("Left");frame.getContentPane().add(t1,BorderLayout.LINE_START);JTextField t2 = new JTextField("Right");frame.getContentPane().add(t2,BorderLayout.LINE_END);JButton b3 = new JButton("Centre");frame.getContentPane().add(b3,BorderLayout.CENTER );frame.pack();frame.setVisible(true);
![Page 16: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/16.jpg)
Grid Layout
JFrame.setDefaultLookAndFeelDecorated(true);JFrame frame = new JFrame("Grid");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new GridLayout(4,3,5,5));for (int i=0; i<10; i++) frame.getContentPane().add(new JButton(""+i));frame.pack();frame.setVisible(true);
![Page 17: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/17.jpg)
Swing has a lot of classes
controls
User I/O widgets eg JButton
containers
things that hold other thingseg JFRame
![Page 18: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/18.jpg)
Containers
general purpose containers - panel scroll pane split pane tabbed pane tool bar
top level containers - JFrame JApplet JDialog
![Page 19: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/19.jpg)
JPanel ( in createAndShowGUI)
JFrame.setDefaultLookAndFeelDecorated(true);JFrame frame = new JFrame("I am a JFrame");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setBounds(20,30,300,100);frame.setLayout(null);//Create a panel JPanel myPanel = new JPanel();myPanel.setBackground(new Color(255,3,25));myPanel.setOpaque(true); //Make it the content pane.frame.setContentPane(myPanel);frame.setVisible(true);
![Page 20: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/20.jpg)
Tooltip and border
myPanel.setOpaque(true); myPanel.setToolTipText("I'm a JPanel");myPanel.setBorder(BorderFactory.createLineBorder(Color.white)); frame.setContentPane(myPanel); ..
![Page 21: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/21.jpg)
JSplitPane
setLayout(null);
//Create a split pane
JSplitPane myPane = new JSplitPane();
myPane.setOpaque(true);
frame.setContentPane(myPane);
frame.setVisible(true);
![Page 22: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/22.jpg)
JSplitPane with JPanels
//Create a split paneJSplitPane myPane = new JSplitPane();myPane.setOpaque(true);myPane.setDividerLocation(150);// make two panelsJPanel right = new JPanel();right.setBackground(new Color(255,0,0));JPanel left = new JPanel();left.setBackground(new Color(0,255,0));// set as left and right in splitmyPane.setRightComponent(right); myPane.setLeftComponent(left);
![Page 23: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/23.jpg)
Exercise
![Page 24: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/24.jpg)
JDBC (JAVA DATABASE CONNECTIVITY)
![Page 25: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/25.jpg)
Database and Database Management System
Database is simply a collection of data. In relational database, data is organized into tables.
Database Management System (DBMS) is software to maintain and utilize the collections of data (Oracle, DB2, MySQL)
Student_ID Name Major Grade
101 Shannon BCB A
102 Mike BBMB A
103 Wang MCDB A
… … …
![Page 26: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/26.jpg)
MySQL Introduction MySQL is a database management system SQL stands for the Structured Query Language.
It defines how to insert, retrieve, modify and delete data
Free from www.mysql.com
![Page 27: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/27.jpg)
Introductions
JDBC is Java application programming interface
Allows the Java programmers to access database management system from Java code.
To execute SQL statements
![Page 28: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/28.jpg)
Components of JDBC
JDBC has four Components:
1. The JDBC API.2. The JDBC Driver Manager.3. The JDBC Test Suite.4. The JDBC-ODBC Bridge.
![Page 29: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/29.jpg)
Type 1 JDBC Architecture
![Page 30: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/30.jpg)
Type 2 Java to Native API
![Page 31: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/31.jpg)
Type 3 Java to Network Protocol Or All- Java Driver
![Page 32: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/32.jpg)
Type 4 Java to Database Protocol
![Page 33: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/33.jpg)
Common SQL statements
SQL Select statement:
The SELECT statement is used to select data from a table.
Syntax: Select column_names FROM table_name;
![Page 34: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/34.jpg)
SQL INSERT Statement
To insert a single or multiple records into the database. Syntax: Insert into table_name values(value1,value2..);The Insert statement has mainly three clauses.1). Insert: It specifies which table column has to be inserted in the table.2). Into : It tells in which the data will be stored.3). Values: In this we insert the values we have to insert.
![Page 35: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/35.jpg)
UPDATE Statement
The Update statement is used to modify the data in the tableThe syntax is :
UPDATE table_name Set colunm_name = new_value WHERE column_name = some_name;
![Page 36: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/36.jpg)
DELETE Statement
This delete statement is used to delete rows in a table.
Systax:
DELETE FROM table_name WHERE column_name = some_name;
![Page 37: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/37.jpg)
Login
mysql –h hostname –u username –p [password]
Example% mysql -u usrname -pEnter password: passowrdWelcome to the MySQL monitor. Commands
end with ; or \g. Your MySQL connection id is 23 to server version: 3.23.41.
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
![Page 38: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/38.jpg)
Login Example
Exp:2mysql –h 131.131.0.103 –u amudha –p amudha1
mysql>
![Page 39: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/39.jpg)
mysql> show databases; (What are the current databases at the server?)+--------------+| Database |+--------------+ | mysql | mysql is a database (stores users’ password …) used by system. +--------------+mysql> create database MyDB;(Create a database (make a directory) whose name is MyDB)mysql> use MyDB;(Select database to use )Database changedmysql> show tables;(What tables are currently stored in the MyDB database? )Empty set (0.00 sec)
Create Database
![Page 40: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/40.jpg)
CREATE TABLE Table_Name (column_specifications) Example
mysql> CREATE TABLE student
-> (
-> student_ID INT UNSIGNED NOT NULL,
-> name VARCHAR(20) NOT NULL,
-> major VARCHAR(50),
-> grade VARCHAR(5)
-> );
Query OK, 0 rows affected (0.00 sec)
Student_ID Name Major Grade
Create Table
![Page 41: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/41.jpg)
mysql> show tables;+--------------------+| Tables_in_MyDB |+--------------------+| student |+--------------------+1 row in set (0.00 sec)mysql> describe student;+---------------+----------------------+------+------+----------+--------+| Field | Type | Null | Key | Default | Extra |+---------------+----------------------+-------+-----+-----------+-------+| student_ID | int(10) unsigned | | | 0 | || name | varchar(20) | | | | || major | varchar(50) | YES | | NULL | || grade | varchar(5) | YES | | NULL | |+---------------+----------------------+-------+------+----------+-------+4 rows in set (0.00 sec)
Display Table Structure
![Page 42: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/42.jpg)
ALTER TABLE table_name Operations
mysql> alter table student add primary key (student_ID);Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;+---------------+--------------------- +-------+------+----------+-------+| Field | Type | Null | Key | Default | Extra |+---------------+----------------------+-------+------+----------+-------+| student_ID | int(10) unsigned | | PRI | 0 | || name | varchar(20) | | | | || major | varchar(10) | YES | | NULL | || grade | varchar(5) | YES | | NULL | |+---------------+----------------------+-------+------+-----------+-------
+4 rows in set (0.00 sec)
Modify Table Structure
![Page 43: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/43.jpg)
INSERT INTO table_name SET col_name1=value1, col_name2=value2, col_name3=value3, …
Example
mysql> INSERT INTO student SET student_ID=101, name='Shannon', major='BCB', grade='A';
Query OK, 1 row affected (0.00 sec)Student_ID Name Major Grade
101 Shannon BCB A
Insert Record
![Page 44: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/44.jpg)
SELECT what_columns
FROM table or tables
WHERE condition
Example
mysql> SELECT major, grade FROM student WHERE name='Shannon';
+-------+-------+| major| grade|+-------+-------+| BCB | A |+-------+-------+1 row in set (0.00 sec)
mysql> SELECT * FROM student;
Student_ID Name Major Grade
101 Shannon BCB A
102 Mike BBMB A
103 Wang MCDB A
… … …
Retrieve Record
![Page 45: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/45.jpg)
UPDATE table_name
SET which columns to change
WHERE condition Examplemysql> UPDATE student SET grade='B' WHERE name='Shannon';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM student WHERE name=‘Shannon’;
+------------+---------------+--------+--------+
| name | student_ID | major | grade |
+------------+---------------+--------+--------+
| Shannon | 101 | BCB | B |
+------------+---------------+--------+--------+
1 row in set (0.00 sec)
Update Record
![Page 46: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/46.jpg)
DELETE FROM table_name WHERE condition
Examplemysql> DELETE FROM student
WHERE name='Shannon';Query OK, 1 row affected (0.00
sec)
Mysql> DELETE FROM student;
Will delete ALL student records!
Delete Record
![Page 47: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/47.jpg)
Steps to creatd JDBC
Tables creationLoad the driver managerConnection establishmentStatement creationsProcess the result setExecute the statementClose the connection and statementStop the program
![Page 48: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/48.jpg)
Creating a Database Table
String table ;table= "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))"; st.executeUpdate(table);
![Page 49: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/49.jpg)
Connection Establishment
Is an interface in java.sql package Specifies connection with specific database like: MySQL, Ms-Access, Oracle etc and java files.The SQL statements are executed within the context of the Connection interface.
Class.forName(String driver):
![Page 50: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/50.jpg)
Connection Establishment
Syntax
Connection conn; Class.forname(“com.mysql.jdbc.Driver”).newInstance();
![Page 51: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/51.jpg)
DriverManager
It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be register with this class.getConnection(String url, String userName, String password): url: - Database url where stored or created your database userName: - User name of MySQL password: -Password of MySQL
![Page 52: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/52.jpg)
Load the JDBC Driver into the database
Syntax
String userName = "root";
String password = "mysql";
String url = "jdbc:mysql://localhost/programs";
conn = DriverManager.getConnection(url,userName,password);
![Page 53: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/53.jpg)
Create the statements and update the values in the table structure
Statement st=conn.createStatement(); st.execute("create table stud10(rollno
int,name text,m1 int,m2 int)"); st.executeUpdate("insert into stud10
values(2,'rani',50,90)");
![Page 54: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/54.jpg)
Process the Query result set
rs=st.executeQuery("select * from stud10");
while(rs.next())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getInt(3));
System.out.println(rs.getInt(4));
}
![Page 55: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/55.jpg)
Close the connection
con.close():This method is used for disconnecting the connection. It frees all the resources occupied by the database.
![Page 56: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/56.jpg)
Close the statement and connection
st.close(); conn.close();
![Page 57: Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649f1f5503460f94c38003/html5/thumbnails/57.jpg)
Final Steps
To import Syntax
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.6.jar
To Compile
Javac Filename.java To Run
java Filename