Home Answers Viewqa Java-Beginners how to connect swings with jdbc...

 
 


mohammed afsar
how to connect swings with jdbc...
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
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, Follow..."); Connection conn = DriverManager.getConnection("jdbc:odbc:excel
Use JDBC to connect Microsoft Access
Use JDBC to connect Microsoft Access  How to use JDBC to connect Microsoft Access
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 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
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 raja.chaya@gmail.com
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
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 the following code: import java.awt.*; import javax.swing.*; import java.awt.event.
Connect database in Access to Netbean?
Connect database in Access to Netbean?  how to connect database in micrsoft access to Netbean?i know it can be connected by using JDBC-ODBC bridge, can i know the steps in connecting the database? Is there any source code
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
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...("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql
JDBC
JDBC  How to connect JAVA Servlet with the database
jdbc
how to connect JSP page to database - JDBC  how to connect JSP page to database ?give program
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 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
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
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
Connect JSP with mysql
; This query creates database 'usermaster' in Mysql. Connect JSP with mysql : Now in the following jsp code, you will see how to connect... Connect JSP with mysql   
JDBC
retrieve the value from database into dropdown list using JDBC SQL 2005  How to retrieve the value from database into dropdown list using JDBC &..."); Connection connect = DriverManager.getConnection("jdbc:mysql://localhost
Connect to the database
Connect to the database  how to connect to the database in the iphone
jdbc - JDBC
management so i need how i can connect the pgm to database by using jdbc...? if u replyed its very useful for me...  Hi, Please read JDBC tutorial... can use following code to connect to Database with the help of JDBC API
how to connect to MS access database in JSP?
how to connect to MS access database in JSP?  how to connect to MS access database in JSP? Any seetings/drivers need to be set or installed before... = DriverManager.getConnection("jdbc:odbc:student"); Statement st=con.createStatement
jdbc - JDBC
jdbc  Hi, Could you please tell me ,How can we connect to Sql server through JDBC. Which driver i need to download. Thank You   Hi Friend, Please visit the following code: http://www.roseindia.net/jdbc
how to connect jsp to mysql - Java Beginners
how to connect jsp to mysql  I m new in Mysql and JSP i m ceating.java file using bean file for connectivity but i m not able to connect jsp... me hw to conncet jsp with mysql this is connection file package connect
how to connect j2me program with mysql using servlet?
how to connect j2me program with mysql using servlet?  my program...(); String userid=connect(user.toLowerCase().trim(), pwd.toLowerCase().trim... String connect(String user,String pwd){ String db="mobileapp
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
how to connect webcam in html
how to connect webcam in html  how to connect webcam in html.emphasized text
ex. connect to Oracle - Java Beginners
ex. connect to Oracle  dear sir, I want to ask how to connect java to oracle, please give me a details tutorial with example code how to connect..."; String db = "XE"; String url = "jdbc:oracle:thin:@" + serverName
JSP & Swings
JSP & Swings  How to integrate jsp and swings
How to connect mysql with jsp
How to connect mysql with jsp  how to connect jsp with mysql while using apache tomcat
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
jdbc - JDBC
jdbc  How to do connectivity with java?  Hi friend...[] args) { System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName
how to create frame in swings
how to create frame in swings  how to create frame in swings
swings question
swings question  how to change the background color with the help of color values by using swings
swings - Java Beginners
swings   how t upload images in swings. thanks   Hello Friend, Try the following code: import java.io.*; import java.sql....); try{ Connection con = null; String url = "jdbc:mysql
JDBC - JDBC
JDBC  how can i do jdbc through oracle.. pls if u can send me d.... thanking u santosh.  Hi Friend, Use JDBC with Oracle Follow...) Load and Register the JDBC driver:*********** DriverManager.registerDriver(new
jdbc - JDBC
jdbc  how to get tablecount in jdbc  hai frnd... wat do u... in a database System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName
How can I connect my database to my application ?
How can I connect my database to my application ?  How can I connect my database to my application?   Hi, You can use JDBC API to connect to database from your Java application. Here is the sample code to connect
swings - Java Beginners
"); Connection connect =DriverManager.getConnection("jdbc:mysql://localhost
JDBC Connection code to connect servlet with oracle.
JDBC Connection code to connect servlet with oracle.  JDBC Connection code to connect servlet with oracle
swings for webnms
swings for webnms  if i am expanding node of jtree then i want to collapse previous expanding node of jtree in swings how is it possible
Using Network Address To Connect to a Database
.style1 { text-align: center; } How To Use Network Address To Connect... name is 127.0.0.1. A JDBC URL provides a way to identify a database host, so... to connect a MySql database with your application over a network then you must load

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.