how to connect swings with jdbc...

how to connect swings with jdbc...

View Answers

July 10, 2009 at 1:01 PM

Hi Friend,

We are providing you a code that will insert and retrieve data from MySql database using java swings:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

class Form extends JFrame
{
JButton ADD,RETRIEVE;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Form()
{
final Vector columnNames = new Vector();
final Vector data = new Vector();

label1 = new JLabel();
label1.setText("Employee Name:");
text1 = new JTextField(20);

label2 = new JLabel();
label2.setText("Employee Address:");
text2 = new JTextField(20);

ADD=new JButton("ADD");
RETRIEVE=new JButton("RETRIEVE");

panel=new JPanel(new GridLayout(3,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(ADD);
panel.add(RETRIEVE);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test";, "root", "root");
Statement st = con.createStatement();
int i= st.executeUpdate("Insert into employee(empName,empAddress) values('"+value1+"','"+value2+"')");
JOptionPane.showMessageDialog(null,"Data is successfully inserted into the database." );
st.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
});
RETRIEVE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test";, "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from employee");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
st.close();
}
catch(Exception e) {}
JFrame tab=new JFrame();
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
tab.add( scrollPane );
tab.setVisible(true);
tab.setSize(300,100);
}
});
}
}
class InsertAndRetrieve
{
public static void main(String arg[])
{
try
{
Form frame=new Form();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
how to connect swings with jdbc... - Java Beginners
how to connect swings with jdbc...  Hi friends, Is it possible, to connect swings with data base like oracle....i want to store data into database... by using servlets and jsp but is it possible by using swings........ for data base
how to connect mysql with JDBC - JDBC
how to connect mysql with JDBC  I have created three tables in the database MYsql, i have to connect them now using JDBC, can u please suggest me...(); } } Thanks Rajanikant  Hi friend, To mysql connect using JDBC
Advertisements
how to connect jdbc
how to connect jdbc  package com.tcs.ilp.Try.Controller; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import
how to connect xlsx(2007 excel) - JDBC
how to connect xlsx(2007 excel)  i am not able connect to office 2007 excel file from jdbc
what is ment by jdbc and how to connect with database?
what is ment by jdbc and how to connect with database?  i want answer for this question
Swings and JDBC
Swings and JDBC  Hi.. I am vinay.. I am developing a small application using swings and mysql. I am sending part of the code here.. The problem is i... created JTextfields... but i am not getting how i can fetch those particular
Swings - JDBC
Swings  Hi..Sir...I am begginer in Netbeans... How to retrieve the databases from sqlserver to Jtable in Netbeans... In my form..i have three fields like..Number,Name,PhoneNo(3 Labels & 3 textfields),(Save)1 button & Jtable
Use JDBC to connect Microsoft Access
Use JDBC to connect Microsoft Access  How to use JDBC to connect Microsoft Access
How to connect with database
How to connect with database  jdbc connection
connect jdbc to an Excel spreadsheet
connect jdbc to an Excel spreadsheet  hello, How can I connect to an Excel spreadsheet file using jdbc?   Hello Friend,ADS_TO_REPLACE_1...("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc
could not connect to the server - JDBC
could not connect to the server  Hii sir, I am working with a JBDC program now. Now i am trying to host it. when i am trying to connect...: Connection timed out: connect STACKTRACE: java.net.SocketException
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need..."); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register...("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need..."); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register...("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc
how to create frame in swings
how to create frame in swings  how to create frame in swings
not able to connect to mysql using.. jdbc
not able to connect to mysql using.. jdbc   i am not able to connect to mysql using jdbc .. is there any classpath that i need to set..because i am using mysql-connector-java jar file..to connect to mysql.. Pls provide the steps
cannot connect to database - JDBC
cannot connect to database  Iam using eclipse in my system ,when connecting the database mysql version 5.0 to the eclipse iam getting an error as ""Creating connection to mysql has encountered a problem.Could not connect to mysql
How to connect to MySQL in JSP?
How to connect to MySQL in JSP?  How to connect to MySQL in JSP..., You can use the Java JDBC code to connect to MySQL database in JSP page. Check the tutorial: Connect JSP with mysql Thanks
How to connect
How to connect  how to connect to a remote host using jsp? We need to update files to the database of the application on the local machine from the updated database on our webpage
JDBC Connection code to connect servlet with oracle.
JDBC Connection code to connect servlet with oracle.  JDBC Connection code to connect servlet with oracle
how to connect webcam in html
how to connect webcam in html  how to connect webcam in html.emphasized text
swings - JDBC
How to connect mysql with jsp
How to connect mysql with jsp  how to connect jsp with mysql while using apache tomcat
how can we store the encrypted passwaord in swings?
how can we store the encrypted passwaord in swings?   how can we store the encrypted passwaord in swings
Connect to MS Acces wothout using ODBC but JDBC - JDBC
Connect to MS Acces wothout using ODBC but JDBC  Hi, I want to connect my MS Access using JDBC but not ODBC. Please help me out. Thanks
SWINGS
SWINGS  WHAT ARE THE DIFFERENCES BETWEEN AWT AND SWINGS
How to create form in Swings
How to create form in Swings  How to create registration, login and question form in Java Swing?   Creating different forms in Java Swing - Examples how to create registration form in swing swing login form example
can't connect to MySQL Server(10060) - JDBC
can't connect to MySQL Server(10060)  Hii Sir, I am working... MySQL front end. Can we connect to the MySQL server using the I.P address... write as the subject. Can we connect thorugh a MySQL Frontend. Kindly
how to add audio clip in swings with event handling
how to add audio clip in swings with event handling  hello dear, i wanna to play audio clip in java program in JFrame in swings by event handling... how it can be done...... thanks alot
how i connect two page
how i connect two page  strong textlink text
How to create XML from Swings
How to create XML from Swings  How to create XML using Swings. I have a Swing GUI and capturing all data from it.When i click on submit, an xml should be created with proper binding for all the elements. < Student > <
how to connect program to database
how to connect program to database  Sir i want ur help in my program. I want to save the data in the database. When i'll click on save button the entered data must be save in the database. I want an action performed method
How to connect to MySql Database from Servlet?
; font-weight: bold; } How to connect to MySql Database from Servlet... will show you how to connect to MySQL database and perform select operation. You will learn the JDBC steps necessary to connect to the MySQL Database and execute
How to connect on screen keyboard to system.
How to connect on screen keyboard to system.  How to connect on screen keyboard to system.I made on screen keyboard in java(swing).Now i want connect to system means when i pressed any button then it type on notepad or any
How to connect on screen keyboard to system.
How to connect on screen keyboard to system.  How to connect on screen keyboard to system.I made on screen keyboard in java(swing).Now i want connect to system means when i pressed any button then it type on notepad or any
another frame by using awt or swings
another frame by using awt or swings  how to connect one frame to another frame by using awt or swings
swings
swings  how to disable the labels.i am using the setenable(false) method to disable the label, but still mouse events(mouse clicked) are working on that label, how can i deactivate the mouse events also on the label
swings
swings  My program contains more one JComboBoxes how can i selectitems in my program. Please answer me my id [email protected]
swings
swings  Thank you deepak it is fine.... I think you have gone through mine sent code in that i am not able add that Textfield dynamically. Can youhelp me how can i add textfields dynamically in my code lookin forward to hear
how to connect mysql from gui?
how to connect mysql from gui?  i am unable to connet to mysql server from my projet that is gui for mysql...can anyone help me in this...its urgent...("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st
swings
swings  i have 20 labels in mappanel ,if i click on the one label ,how to know which one is selected in this...  Hi Friend, Try...","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; for(i = 0
how to connect two swing frames
how to connect two swing frames  how to connect two swing frames   Hi Friend, Try this: 1)LoginDemo.java: import java.sql.*; import...("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc
JSP & Swings
JSP & Swings  How to integrate jsp and swings
how to display a table and buttons in swings - Java Beginners
how to display a table and buttons in swings  Hi frends, Actually i want to display a table during runtime and at the same time i also want to add different buttons below this displayed table using swings.....please can any
how to connect SQL Server 2005 using php
how to connect SQL Server 2005 using php  i need to connect SQL Server 2005 using php. how can i connect . how to use mssql_connect function
swings question
swings question  how to change the background color with the help of color values by using swings
swings:how to link from one form to another form
swings:how to link from one form to another form  how to link from one form to another form
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?  HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?  HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE
how to connect client to server using Sockets
how to connect client to server using Sockets  how to connect client to server using Sockets
how to connect two databse through HibernateFramework
how to connect two databse through HibernateFramework  how to connect two databse through HibernateFramework

Ads