how to create jdbc connection - wordpress.com · 2017-04-28 · 1 jdbc connection (net beans ide...

12
1 JDBC Connection (Net Beans IDE 6.9) with MySQL www.dbgyan.wordpress.com How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open Net Bean IDE 6.9 Create New Project Choose Categories Java and Projects Java Application Give the Project Name and Location of Project Right Click Project Library and Choose Add Library Select MySQL JDBC Driver And Click ADD Library

Upload: others

Post on 28-Jul-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

1

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

How to create JDBC Connection (Net Beans IDE 6.9) with MySql.

Open Net Bean

IDE 6.9

Create New Project

Choose Categories Java and

Projects Java Application

Give the Project Name and

Location of Project

Right Click

Project Library

and Choose

Add Library

Select MySQL JDBC Driver

And Click ADD Library

Page 2: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

2

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Drag Panel from Palette

Add New Frame

Give Frame

Name

After adding

new frame

I am palette

(swing control)

Container

Drag Panel from

Palette

I am Project

Window

Right Click panel and choose change

variable name from open MENU

Page 3: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

3

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Give name of panel

i.e. panelTop

Choose background property from

property window and Select any

Color from Color chooser for

panel

Right click panel

and choose Edit

Text from Menu

Choose and Drag

Swing Control

JtextField and

JLebel , Edit

Text and change

variable name

Right click and choose

Property and choose

font ,set font size and

type

After adding JtextFiled and

jLebel

Page 4: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

4

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Insert all jLables, jButtons, jTextField, jRadioButtons,

jComboBoxes, jTextArea, jFormatted Field

Page 5: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

5

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Click on services and Right Click on

Database then choose Register MySQL

Server

Right Click MySQL Server at

localhost :3306[root] and Choose

Create Database from Menu

Give name of the

Database

Right Click MySQL Server at

localhost :3306[root] and Choose

Execute Command from Menu Write these

SQL

Commands

in once and

then press

Ctrl+Shift+e

Click on main frame and the

go to directly his source code

Page 6: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

6

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Write this line

under package of

project

Create object of

Connection as con ,

Statement as stmt,

ResultSet as rs and

initialize object as null

Write up to database name

Write this code from services

Write this code first (try

and catch block)

Page 7: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

7

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

private void btnInsertActionPerformed(java.awt.event.ActionEvent evt)

{

try

{

String sql=null;

if(rbnMale.isSelected()==true)

{

sql="insert into Student_t values

("+txtAdmNo.getText()+",'"+txtName.getText()+"',

'"+txtFName.getText()+"','"+txtMName.getText()+"','"+txtFDOB.getText()+"','"

+cboClass.getSelectedItem()+"','"+cboSection.getSelectedItem()+"','M','"+cboS

tream.getSelectedItem()+"','"+txtAreaAddress.getText()+"');";

}

else

{

sql="insert into Student_t values

("+txtAdmNo.getText()+",'"+txtName.getText()+"',

'"+txtFName.getText()+"','"+txtMName.getText()+"','"+txtFDOB.getText()+"','"

+cboClass.getSelectedItem()+"','"+cboSection.getSelectedItem()+"','F','"+cboSt

ream.getSelectedItem()+"','"+txtAreaAddress.getText()+"');";

}

int S=stmt.executeUpdate(sql);

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, e);

}

}

Click on design view

Right Click on Insert button

and choose Event ->

Action -> actionPerformed

Page 8: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

8

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

try

{

String qry=null;

if(rbnMale.isSelected()==true)

{

qry="Update Student_t SET

AdmNo="+txtAdmNo.getText()+",Name='"+txtName.getText()+"',FName='"+tx

tFName.getText()+"',MName='"+txtMName.getText()+"',DOB='"+txtFDOB.getT

ext()+"',Sclass='"+cboClass.getSelectedItem()+"',Section='"+cboSection.getSel

ectedItem()+"',Gender='M',Stream='"+cboStream.getSelectedItem()+"',Addres

s='"+txtAreaAddress.getText()+"'WHERE AdmNo="+txtAdmNo.getText()+";";

}

if(rbnFemale.isSelected()==true)

{

qry="Update Student_t SET

AdmNo="+txtAdmNo.getText()+",Name='"+txtName.getText()+"',FName='"+tx

tFName.getText()+"',MName='"+txtMName.getText()+"',DOB='"+txtFDOB.getT

ext()+"',Sclass='"+cboClass.getSelectedItem()+"',Section='"+cboSection.getSel

ectedItem()+"',Gender='F',Stream='"+cboStream.getSelectedItem()+"',Address

='"+txtAreaAddress.getText()+"'WHERE AdmNo="+txtAdmNo.getText()+";";

}

stmt.executeUpdate(qry);

JOptionPane.showMessageDialog(null, "Record is updated ......");

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, e);

}

}

Right Click on Update

button and choose Event ->

Action -> actionPerformed

Page 9: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

9

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Right Click on Clear

button and choose

Event -> Action ->

actionPerformed

(To Clear all

textfileds,radiobutto

ns,combo boxes)

Right Click on First

button and choose

Event -> Action ->

actionPerformed

(To Move first

Record)

Page 10: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

10

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Right Click on Previous

button and choose Event -

> Action ->

actionPerformed

(To Move Previous Record)

Right Click on Next button and

choose Event -> Action ->

actionPerformed

(To Move Next Record)

Page 11: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

11

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Right Click on Last

button and choose

Event -> Action ->

actionPerformed

(To Move Last

Record)

Right Click on Exit

button and choose

Event -> Action ->

actionPerformed

(To Close /exit the

frame)

Page 12: How to create JDBC Connection - WordPress.com · 2017-04-28 · 1 JDBC Connection (Net Beans IDE 6.9) with MySQL How to create JDBC Connection (Net Beans IDE 6.9) with MySql. Open

12

JDBC Connection (Net Beans IDE 6.9) with MySQL

www.dbgyan.wordpress.com

Press shift +F6 and RUN your program

Download from

www.dbgyan.wordpress.com

version 1.0