Saturday 23 July 2011

Blogger Buzz: Blogger integrates with Amazon Associates

Blogger Buzz: Blogger integrates with Amazon Associates

How to update Record in database

   Connection con=null;
        Statement smt=null;
        con=(Connection)Fun.Connect();//its function to make in other class
      
        
        try {
                smt=con.createStatement();
               
                smt.executeUpdate(" DELETE FROM APP.SYSACC  WHERE COERID ='"+jTextFieldCoerId.getText()+"'");
               
                smt.close();
                con.close();
               JOptionPane.showMessageDialog(this,"You Sucessfully Delete the Account");
                jButtonClear.doClick();
            }
        catch(SQLException ex)
        {
        Fun.DiloagboxError(ex.getMessage());
       
        }

How to make Dialog Box in java

   public void Diloagbox( Object my)
    {
      int re=JOptionPane.showInternalConfirmDialog(my,"Are You Want to Close","Choose yes or no",JOptionPane.YES_NO_OPTION);
     if(re==JOptionPane.YES_OPTION)
    {
         my.dispose();     }
    }
    public void DiloagboxError(String my)
    {
    javax.swing.JPanel jp=new javax.swing.JPanel();
        JOptionPane.showMessageDialog((Component)null,my,"Please Fill Correct Information",JOptionPane.ERROR_MESSAGE);
      
    }
public void MessageBox(String my)
{
  JOptionPane.showMessageDialog(this,my);
             
}

Thursday 14 July 2011

How to set an image as background using Java on Netbeans

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class NewClass3 {

  public static void main(String[] args) {
    ImagePanel panel = new ImagePanel(new ImageIcon("C:\\myimage.jpg").getImage());

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }
}

Friday 8 July 2011

Derby Network Server

Introduction

As explained in the "Embedded Derby" section, an application can embed Derby, which means that the Derby engine runs in the same JVM as the application.
However, an application can also access a Derby database using the more familiar client/server mode. This is achieved via a framework that embeds Derby and handles database requests from applications, including applications running in different JVMs on the same machine or on remote machines. The Derby Network Server is such a framework. It embeds Derby and manages requests from network clients, as depicted in Figure 2.
Figure 2: Derby Network Server Architecture
Figure 2: Derby Network Server Architecture
This section shows how to start the Derby Network Server, configure your environment to use the Derby Network Client JDBC driver, and compile and run a simple Java application that uses the network server. The information presented here is minimal, just enough to help get new users started. For complete information, see part one of the Derby Server and Administration Guide.

Derby Network Server

First set the DERBY_INSTALL environment variable.

Configure environment

To start or stop the Network Server, set your CLASSPATH to include the jar files listed below:
  • derbynet.jar: contains the code for the Derby Network Server and a reference to the engine jar file (derby.jar)
  • derbytools.jar: contains Derby tools
You can set your CLASSPATH explicitly with the command shown below:
Windows: C:\> set CLASSPATH=%DERBY_INSTALL%\lib\derbytools.jar;%DERBY_INSTALL%\lib\derbynet.jar;.
UNIX: $ export CLASSPATH=$DERBY_INSTALL/lib/derbytools.jar:$DERBY_INSTALL\lib\derbynet.jar:.
You can also use the script that the Derby software distribution provides to set CLASSPATH. Change directory to the DERBY_INSTALL/bin directory, then execute setNetworkServerCP.bat (Windows) or setNetworkServerCP.ksh (UNIX), as shown below:
Windows: C:\> cd %DERBY_INSTALL%\bin C:\Apache\db-derby-10.4.1.3-bin\bin> setNetworkServerCP.bat
UNIX: $ cd $DERBY_INSTALL/bin $ . setNetworkServerCP.ksh

Start Network Server

Start the Network server by executing the startNetworkServer.bat (Windows) or startNetworkServer.ksh (UNIX) script. This will start the Network Server up on port 1527 and echo a startup message:
Windows: C:\Apache\db-derby-10.4.1.3-bin\bin> startNetworkServer.bat Security manager installed using the Basic server security policy. Apache Derby Network Server - 10.4.1.3 - (648739) started and ready to accept connections on port 1527 at 2008-04-28 17:13:13.921 GMT
UNIX: $ startNetworkServer.ksh Security manager installed using the Basic server security policy. Apache Derby Network Server - 10.4.1.3 - (648739) started and ready to accept connections on port 1527 at 2008-04-30 09:35:55.871 GMT
Messages will continue to be output to this window as the Network Server processes connection requests.
The Network Server starts Derby, so you'll find the derby.log error log in the directory where you start the Network Server.

An easier way: derbyrun.jar

Furthermore, it is much easier to start Network Server now than before, due to various jar file improvements. With the latest releases, the entire sections "Configure environment" and "Start Network Server" could actually be replaced with just one command line:
Windows: C:\Apache\db-derby-10.4.1.3-bin\lib> java -jar derbyrun.jar server start Security manager installed using the Basic server security policy. Apache Derby Network Server - 10.4.1.3 - (648739) started and ready to accept connections on port 1527 at 2008-04-28 17:13:13.921 GMT
UNIX: $ java -jar derbyrun.jar server start Security manager installed using the Basic server security policy. Apache Derby Network Server - 10.4.1.3 - (648739) started and ready to accept connections on port 1527 at 2008-04-30 09:35:55.871 GMT

Sample Application

Open a new window and set the DERBY_INSTALL environment variable.

Configure environment to use Derby Network Client JDBC driver

To use the Derby Network Client JDBC driver, set your CLASSPATH to include the jar files listed below:
  • derbyclient.jar: contains the JDBC driver
  • derbytools.jar: optional, provides the ij tool
You can set your CLASSPATH explicitly with the command shown below:
Windows: C:\> set CLASSPATH=%DERBY_INSTALL%\lib\derbyclient.jar;%DERBY_INSTALL%\lib\derbytools.jar;.
UNIX: $ export CLASSPATH=$DERBY_INSTALL/lib/derbyclient.jar:$DERBY_INSTALL/lib/derbytools.jar:.
You can also use the script that the Derby software distribution provides to set CLASSPATH. Change directory to the DERBY_INSTALL/bin directory, then execute the setNetworkClientCP.bat (Windows) or setNetworkClientCP.ksh (UNIX) script as shown below:
Windows: C:\Apache\db-derby-10.4.1.3-bin\bin> setNetworkClientCP.bat
UNIX: $ . setNetworkClientCP.ksh

Test network server connection with ij

An embedded connection URL that creates and connects to a database looks like this:
java org.apache.derby.tools.ij ij> connect 'jdbc:derby:MyDbTest;create=true';
A Derby Network Client connection URL is very close; just add //localhost:1527/ before the database name:
java org.apache.derby.tools.ij ij> connect 'jdbc:derby://localhost:1527/MyDbTest;create=true';
What is the physical location of the newly created database?
If you use the embedded driver, by default the database is created in the same directory in which ij was started up. If you use the Derby Network Client JDBC driver, by default the database is created in the directory where the Network Server was started up; in other words, in DERBY_INSTALL/bin.
Derby provides many ways to specify the actual location. For example, the connection URL below specifies a full path for the database location:
java org.apache.derby.tools.ij ij> connect 'jdbc:derby://localhost:1527//home/bill/DerbyDb/MyDbTest;create=true';
The Developer's Guide provides information about where Derby looks for databases:

Copy sample application

This section uses the same sample application that the "Embedded Derby" section uses.
By default this application runs in embedded mode. If you pass it the derbyclient argument at run time, then it will create and connect to the database using the Derby Network Client JDBC driver instead.

A quick look at the code

The SimpleApp.java application spends most of its time creating a table, inserting data into that table, and selecting the data back, demonstrating many JDBC calls as it does so. This section highlights the JDBC calls that make this specifically a client/server Derby application. The "Embedded Derby" section shows how to turn the same code into an embedded application.

Load the Client JDBC Driver

When executed with the derbyclient argument, the SimpleApp application loads the Derby Network Client driver with this code:
driver = "org.apache.derby.jdbc.ClientDriver"; ... Class.forName(driver).newInstance();

Get a Network Server Connection

When executed with the derbyclient argument, the SimpleApp application creates and connects to the derbyDB database with this code:
protocol = "jdbc:derby://localhost:1527/"; ... conn = DriverManager.getConnection(protocol + "derbyDB;create=true", props);
That connection URL, fully constructed, looks like this:
jdbc:derby://localhost:1527/derbyDB;create=true

Don't shut Derby down

If you look at the SimpleApp.java code you'll notice that it only shuts Derby down if it's running in embedded mode. When connecting via the Network Server, other applications might be accessing the same database you are; so, don't shut down the databases or Derby.

Compile Sample Application

Compile the sample application as shown below:
javac SimpleApp.java

Run Sample Application

Run the sample application like this:
java SimpleApp derbyclient
You should see the output shown below:
SimpleApp starting in derbyclient mode. Loaded the appropriate driver. Connected to and created database derbyDB Created table derbyDB Inserted 1956 Webster Inserted 1910 Union Updated 1956 Webster to 180 Grand Updated 180 Grand to 300 Lakeshore Verified the rows Dropped table derbyDB Closed result set and statement Committed transaction and closed connection SimpleApp finished
Quick question: Where is the physical location of the derbyDB database that the sample application creates?
(Answer: In the default $DERBY_INSTALL/bin location.)

Stop Network Server

Stop the Network server by executing the stopNetworkServer.bat (Windows) or stopNetworkServer.ksh (UNIX) script, as shown below:
Windows: C:\> cd %DERBY_INSTALL%\bin C:\Apache\db-derby-10.4.1.3-bin\bin> setNetworkServerCP.bat C:\Apache\db-derby-10.4.1.3-bin\bin> stopNetworkServer.bat
UNIX: $ cd $DERBY_INSTALL/bin $ . setNetworkServerCP.ksh $ stopNetworkServer.ksh
The window running the NetworkServer should output a message confirming the shutdown.
Note that, as with starting the server, there is also an easier way to shut down the server. For example:
Windows: C:\> java -jar %DERBY_INSTALL%\lib\derbyrun.jar server shutdown
UNIX: $ java -jar $DERBY_INSTALL/lib/derbyrun.jar server shutdown
Run derbyrun.jar without arguments for further usage information. derbyrun.jar has been included with Derby releases since the 10.2 release series.

Next Steps

Network Server Options

By default, the Derby Network Server only accepts requests from the localhost on port 1527. You can modify the Network Server to:
For more information about these options, and more, see the Derby Server and Administration Guide.

Embedded Server

Up until this point, this section has focused on how to start and stop the Network Server as an independent process.
Another option, called the "embedded server", allows an application to load the embedded JDBC driver for its own use and start the Network Server to allow remote access by applications running in other JVMs. The application that does this can access the database with either the embedded driver or the client driver.
Figure 3 depicts an application called MyEmbedSrvApp that loads the embedded driver and also starts up the Network Server. This allows other applications, such as ij or SimpleApp using the Derby Network Client, to connect to the same database via a client JDBC driver.
Figure 3: Derby Embedded Server Architecture
Figure 3: Derby Embedded Server Architecture
While it is certainly possible for MyEmbedSrvApp to use the Derby Network Client JDBC driver, it's probably better for it to use the embedded driver because Derby Network Client JDBC database requests incur the overhead of going out through the network. The embedded server architecture lets the application that embeds Derby retain the advantages of embedded access while also enabling remote access to the same database using another tool, such as ij. Thus, you end up with the best of both worlds: embedded and client/server.
You've reached the end of this Derby tutorial and should now understand how to use Derby in the embedded and Network Server frameworks. The "Overview" section provides suggestions for how you might use Derby with some other products.

How to set an image as background using Java on Netbeans

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class NewClass3 {

  public static void main(String[] args) {
    ImagePanel panel = new ImagePanel(new ImageIcon("C:\\myimage.jpg").getImage());

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

Sunday 3 July 2011

Connect javaDB database to ur Frame

package mydatabase;
import java.sql.*;
/**
 *
 * @author user
 */
public class ClassFun {
    public Connection con=null;
    public Statement smt=null;
    public ResultSet res=null;
    public void Connect()
    {
      try
     {
         Class.forName("org.apache.derby.jdbc.ClientDriver");
       
     }
     catch(java.lang.ClassNotFoundException e)
     {
            System.err.println( e.getMessage());
     }
     try
     {
          con = DriverManager.getConnection("jdbc:derby://localhost:1527/student","username","password");
     }
     catch(SQLException e)
     {
            System.err.println(e.getMessage());
     }
    }   
}

Adding database record in Table components in java

package mydatabase;
import java.sql.*;
/**
 *
 * @author user
 */
public class JFrameRecord extends javax.swing.JFrame {
 ClassFun cf=new ClassFun();
    /** Creates new form JFrameRecord */
    public JFrameRecord() {
        initComponents();
        cf.Connect();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        jLabel2 = new javax.swing.JLabel();
        JtextId = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRecord = new javax.swing.JTable();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel2.setText("College ID");
        JtextId.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });
        jTableRecord.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Roll No", "Student Name", "Father's Name", "Course", "Year", "Semester"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableRecord);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 444, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(28, 28, 28)
                        .addComponent(JtextId, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jButtonSearch)))
                .addContainerGap(139, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(JtextId, 0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonSearch))
                    .addComponent(jLabel2))
                .addGap(56, 56, 56)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(233, 233, 233))
        );
        pack();
    }// </editor-fold>
    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here
        try{
        cf.smt=cf.con.createStatement();
         cf.res = cf.smt.executeQuery("SELECT * FROM APP.STUDB where ID="+Integer.parseInt(JtextId.getText()));
         while (cf.res.next())
         {
            jTableRecord.setValueAt(cf.res.getInt("ID"),0,0);
            jTableRecord.setValueAt(cf.res.getString("SNAME"),0,1);
            jTableRecord.setValueAt(cf.res.getString("FNAME"),0,2);
            jTableRecord.setValueAt(cf.res.getString("COURSE"),0,3);
            jTableRecord.setValueAt(cf.res.getInt("SYEAR"),0,4);
            jTableRecord.setValueAt(cf.res.getString("SEM"),0,5);
           
//      
//          jTableRecord.getColumn(1).setHeaderValue(cf.res.getInt("ID"));
//             // cf.res.getInt("ID");
//            jTableRecord.setValueAt(cf, WIDTH, WIDTH).getColumn(2).setsetValueAt(cf.res.getString("SNAME"));
//            jTableRecord.getColumn(3).setHeaderValue(cf.res.getString("FNAME"));
//            jTableRecord.getColumn(4).setHeaderValue(cf.res.getString("COURSE"));
//            jTableRecord.getColumn(5).setHeaderValue(cf.res.getString("SYEAR"));
//            jTableRecord.getColumn(6).setHeaderValue(cf.res.getString("SEM"));
//          
          
         }
         cf.smt.close();
            cf.res.close();
        }
        catch(SQLException e)
        {
//            cf.smt.close();
//            cf.res.close();
            System.err.println(e.getMessage());
        }
    }                                            
    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify
    private javax.swing.JTextField JtextId;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableRecord;
    // End of variables declaration
}