Add to Favorites    Make Home Page 160 Online  

 Language Categories  
 Our Services  

Home » Java Home » JDBC (Java Database Connectivity) Home » Connecting Java with MS-Access - Inserting data in

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Connecting Java with MS-Access - Inserting data in
Author Aseem
Author Email aseemshrestha [at] hotmail.com
Description Inserting data in Access DataBase from Java frame.
Category Java » JDBC (Java Database Connectivity)
Hits 146670
Code Select and Copy the Code
import java.sql.*; import java.math.*; import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; /* author: aseem shrestha */ /* database connectivity with MS-Access is done by creating DataSourceName(dsn) in this example*/ /* Steps to use this example: * go to ms-access and make a table called "student_base" and give it a file name student_base.mdb * 1. Go to Control Panel 2. Click on Administrative Tools(windows 2000/xp), Click on ODBC(win98) 3. click on ODBC 4. Then , you will see a ODBC dialog box. Click on UserDSn 5. Click on Add Button 6. Select Microsoft Access Driver(*.mdb) driver and click on finish 7. Give a Data Source Name : student_base 8. Then Click on Select 9. Browse on the database name you have created and click it:student_base.mdb is a database file where all data will be stored 10. Click on OK. Once the DSN is created, you can do this example*/ public class AddNewStudent extends JFrame implements ActionListener { private JButton btnok,btnexit,btnaddnew; //buttons private JTextField tf1,tf2;//textfields private JLabel lblname,lbladd,lblmsg; //labels private JPanel p1,p2,p3,psouth; //panels public AddNewStudent() //constructor { //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 tf1 = new JTextField(12); tf2 = new JTextField(12); //initializing labels lblname = new JLabel("Name:"); lbladd = new JLabel("Address:"); lblmsg = new JLabel("",JLabel.CENTER); //initializing panels p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); psouth = new JPanel(); //adding buttons and label to panel p1 //setting flowlayout p1.setLayout(new FlowLayout()); p1.add(btnok); p1.add(btnexit); p1.add(btnaddnew); //adding lblmsg to panel p3 p3.add(lblmsg); //adding both the panels to new panel,psouth //settin layout 2:1 psouth.setLayout(new GridLayout(2,1)); psouth.add(p3); psouth.add(p1); //adding label and textfields to panel p2 p2.setLayout(new GridLayout(3,1)); //setting line and titled border for panel p2 p2.setBorder(BorderFactory.createLineBorder(Color.red)); p2.setBorder(BorderFactory.createTitledBorder("Enter Your Details")); p2.add(lblname); p2.add(tf1); p2.add(lbladd); p2.add(tf2); //adding panel to container this.getContentPane().add(p2,"Center"); this.getContentPane().add(psouth,"South"); this.setSize(300,300); this.setLocation(100,200); this.show(); } public static void main(String args[]) { AddNewStudent ad = new AddNewStudent(); } //event handling public void actionPerformed(ActionEvent e) { if(e.getSource()==btnok) { PreparedStatement pstm; ResultSet rs; String sql; //if no entries has been made and hit ok button throw an error //you can do this step using try clause as well if((tf1.getText().equals("")&&(tf2.getText().equals("")))) { lblmsg.setText("Enter your details "); lblmsg.setForeground(Color.magenta); } else { try { //loading the driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //connection object created using DriverManager class //student_base is the name of the database Connection connect = DriverManager.getConnection("jdbc:odbc:student_base"); //creating prepared statement object pstm so that query can be sent to database pstm=connect.prepareStatement("insert into student_base values(?,?)"); pstm.setString(1,tf1.getText()); pstm.setString(2,tf2.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(); connect.close(); } catch(SQLException sqe) { System.out.println("SQl error"); } catch(ClassNotFoundException cnf) { System.out.println("Class not found error"); } } } //upon clickin button addnew , your textfield will be empty to enter next record if(e.getSource()==btnaddnew) { tf1.setText(""); tf2.setText(""); } if(e.getSource()==btnexit) { System.exit(1); } } }

Related Source Codes

Script Name Author
Sending mail Using JavaMail to Yahoo and Gmail accounts sai prasad
Simple Program in Java to Implement Multithreading Satish.K
Simple Calculator in Java Using Remote Method Invocation Satish.K
Guest Book Application Using Servlets Satish.K
String Manipulation Using Stringification Satish.K
String Manipulation Using Stringification Satish.K
Moving Ball Application Using Java Beans Satish.K
Rapid Roll game subrahmanyeswararao
student mgm arpan
Sourav Datta
Download Manager Sagar
Address Book in Java Rahul Chouhan
address book using java database connectivity(jdbc-msaccess) shekhar bansal
sun Steganography B.Rajavel
Address Book in Java Rahul Chouhan

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:


Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2014 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1160


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review