Results 1 to 2 of 2

Thread: Java Swing and MySQL

  1. #1
    Join Date
    Jul 2009
    Posts
    8

    Exclamation Java Swing and MySQL

    Hi..Iam using theNet beans ide for the code below:
    import java.awt.event.*;
    import java.sql.*;
    public class BarcodeReader extends JFrame implements ActionListener {
    public BarcodeReader() {
    initComponents();
    nb.addActionListener(this);
    }
    public BarcodeReader() {

    initComponents();
    nb.addActionListener(this);
    tble.addActionListener(this);
    eb.addActionListener(this);
    jButton1.addActionListener(this);
    jButton2.addActionListener(this);
    }
    public void jdbcConnect(){
    Connection con=null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "database";
    String driver = "com.mysql.jdbc.Driver";
    String user = "unam";
    String pass = "pwd";
    try{
    con=DriverManager.getConnection(url + db, user, pass);
    con.setAutoCommit(false);
    Statement stmt=con.createStatement();
    Class.forName(driver);

    stmt.execute("INSERT into machine(barcode) VALUES ('"+s+"')");

    con.commit();
    con.close();
    System.out.println("Connection is closed...");
    }catch (Exception ex) {
    System.out.println(ex);

    }}
    //actionlistener for the button in new machine
    public void actionPerformed(ActionEvent e){
    try{
    if(e.getSource()==nb){
    jdbcConnect();
    stmt.executeUpdate("INSERT into machine(mname) values "+jTextField1.getText()+"'");
    }}catch (Exception ex) {
    System.out.println(ex);
    }new NewJFrame().setVisible(true);

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new BarcodeReader().setVisible(true);
    }
    });
    }

    In the actionPerformed method i'm gettin the erro cannot find variable "stmt".
    But i've initialized in the jdbcConnect() method. wth is the pblm here??Plz excuse for the untidy indendtns..

  2. #2
    Join Date
    Aug 2009
    Posts
    12

    Re: Java Swing and MySQL

    Can you tell me why are you initializing the "Statement stmt=con.createStatement();" In the Try { block }?

Similar Threads

  1. Applets V/s Swing in JAVA
    By "Dritan" in forum Software Development
    Replies: 3
    Last Post: 14-12-2010, 09:04 AM
  2. Print in java using Swing
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 09:50 AM
  3. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  4. Message Box example for Java Swing
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 05:30 PM
  5. Java swing help with repaint()
    By grudge in forum Software Development
    Replies: 4
    Last Post: 23-07-2009, 03:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,271,285.86233 seconds with 16 queries