Question: programming

Ads

 
 
 

Share on Google+Share on Google+

nehakhandelwal
programming
2 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
View Answers

August 18, 2008 at 6:26 PM


Hi friend,

import java.sql.*;
import java.math.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class AwtDatabase extends JFrame implements ActionListener {

private JButton btnok,btnexit,btnaddnew;
private JTextField jtext1,jtext2;
private JLabel lblname,lbladd,lblmsg;
private JPanel panel1,panel2,panel3,psouth;

public AwtDatabase(){

//initializing buttons
btnok = new JButton("OK");
btnok.addActionListener(this);
btnexit = new JButton("Exit");
btnexit.addActionListener(this);
btnaddnew = new JButton("AddNew");
btnaddnew.addActionListener(this);

//initializing textfields
jtext1 = new JTextField(10);
jtext2 = new JTextField(10);

//initializing labels
lblname = new JLabel("Student Name:");
lbladd = new JLabel("Student RollNO:");
lblmsg = new JLabel("",JLabel.CENTER);

//initializing panels

panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
psouth = new JPanel();

panel1.setLayout(new FlowLayout());
panel1.add(btnok);
panel1.add(btnexit);
panel1.add(btnaddnew);

//adding lblmsg to panel panel3

panel3.add(lblmsg);
psouth.setLayout(new GridLayout(2,1));
psouth.add(panel3);
psouth.add(panel1);

//adding label and textfields to panel panel2
panel2.setLayout(new GridLayout(3,1));
//setting line and titled border for panel panel2
panel2.setBorder(BorderFactory.createLineBorder(Color.red));
panel2.setBorder(BorderFactory.createTitledBorder("Enter Your Information!"));
panel2.add(lblname);
panel2.add(jtext1);
panel2.add(lbladd);
panel2.add(jtext2);
//adding panel to container
this.getContentPane().add(panel2,"Center");
this.getContentPane().add(psouth,"South");
this.setSize(400,300);
this.setLocation(100,200);
this.show();
}

public static void main(String args[]){
AwtDatabase ad = new AwtDatabase();
}

//event handling
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnok) {
PreparedStatement pstm;
ResultSet rs;
String sql;
if((jtext1.getText().equals("")&&(jtext2.getText().equals("")))) {
lblmsg.setText("Enter your Name & RollNo.");
lblmsg.setForeground(Color.magenta);
}

else{

try{
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";;;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String password="amar123";
//loading the driver
Class.forName(driver);
con = DriverManager.getConnection(url+db,userName,password);
pstm=con.prepareStatement("insert into student_detail values(?,?)");
pstm.setString(1,jtext1.getText());
pstm.setString(2,jtext2.getText());
//execute method to execute the query
pstm.executeUpdate();
lblmsg.setText("Details have been added to database");

//closing the prepared statement and connection object
pstm.close();
con.close();
}
catch(SQLException sqe) {
System.out.println("SQl error");
}
catch(ClassNotFoundException cnf) {
System.out.println("Class not found error");
}}
}
if(e.getSource()==btnaddnew) {
jtext1.setText("");
jtext2.setText("");
}
if(e.getSource()==btnexit){
System.exit(1);
}
}
}



August 18, 2008 at 6:27 PM


And use for MS-Access database

Step for using this example.

* go to ms-access and make a table and give it a file name student.mdb
* go to Control Panel
* click on Administrative Tools, click on ODBC(win98)
* click on ODBC
* Then , you will see a ODbc dialog box.click on UserDSn
* click on add button
* Select microsoft Access Driver(*.mdb) driver and click on finish
* give a data Source name (table name)
* Then click on Select
* Browse on the database name you have created and click it:student.mdb is a database file where all data will be stored

* Click on OK

--------------------------------------

Read for more information.

http://www.roseindia.net/java/example/java/awt/

Thanks.

Ads









Related Tutorials/Questions & Answers:
Programming
Programming  Given a number n, write a programming to determine its square root if it is possible, in the contraly case print an appropriate massege on the screen
programming
Java Constructor programming for single and double constructor  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object
Advertisements
network programming
network programming   Tutorial for Network programming and administration
Programming with JSP
Programming with JSP  Who will post me the answer
Java Programming
Java Programming  Hi, What is Java Programming? How I can learn Java Programming in one month? Thanks
Programming with JSP
Programming with JSP  Write a program using jsp that accepts a course code as input and displays the course title the program to which it belongs
Ajax programming
Ajax programming  Hi, How I can start ajax programming easily? I... concept. What should be my starting point for Ajax programming? Thanks   Hi, If you have prior programming experience in any web development
Programming (general)
Programming (general)  I haven't yet started college/university but I'm interested in studying programming. I wondering if programming is difficult or confusing with all the codes you have to learn? Also how many languages would
programming of foxpro
programming of foxpro  hello sir , sir i am use the foxpro . but i know about c . sir how can i learn fox pro . i know about something about foxpro
programming concept
programming concept  Write a program that allows the user to input a total dollar amount for an online shopping order and computes and outputs the shipping cost based on the following schedule
C Programming
C Programming  hi,how can i write a program in C programming to place reservation in air plane from the menu let the user to inter his/her gender(W or M)and draw seats by using ascii codes and choose where he/she will sit
programming error
programming error  I got an error when i did the following program the prg. is to "select the name from database when the age is given for the respective name" error i got is:SQL exception please give me the solution by providing
Socket Programming
Socket Programming  How to connect a computer through IP only and check whether it is up and running? I can't see any method in Socket programming where I can pass only IP address as argument. I need the pass port number also
programming c#
programming c#   create and make use of at least one class with appropriate instance methods to solve the programming problem. use console-based menus to navigate between the functionality offered by the application. at least one
Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads