You are on page 1of 16

NOSQL

GANPAT UNIVERSITY

U. V. Patel College of Engineering

Practical No: 8

Developing Java Application with NoSQL-DB

2CEIT405: NOSQL

18012011054 1
NOSQL

Enrollment No: 18012011054 Name: yagnesh pansuriya


To make a java application with mongoDB concepts you have to follow this steps:-

STEP-1:- open mongod server.


Step-2:-
Open mongo client server.and create a database test also create a collection student and insert a
record as displayed In below screen shots.

Step-3:-
Now open netbeans and create a new project and a mongo java driver jar file.as will find this jar file
under project section.

Now you have to write below code for connect your application with mongo
db server.

CODE:
package Main;
import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

18012011054 2
NOSQL

import com.mongodb.DBCursor;

import com.mongodb.MongoClient;

import javax.swing.table.DefaultTableModel;

public class MongoDBViewer extends javax.swing.JFrame {

public MongoClient mongo; public DB db;

public DBCollection table;

DefaultTableModel tb;

public MongoDBViewer() {

initComponents();

public void con_db()

try

mongo = new MongoClient("localhost",27017);

catch(Exception e)

System.out.println("some error generated !!!!");

db = mongo.getDB("test");

tb = (DefaultTableModel) jTable1.getModel();

table = db.getCollection("student");

tb.setRowCount(0);

DBCursor cr = table.find();

while(cr.hasNext())

18012011054 3
NOSQL

BasicDBObject obj = (BasicDBObject) cr.next();

Object[] ROW = {obj.getString("fname"), obj.getString("lname")};

tb.addRow(ROW);

/**

* 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() {

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jTextField1 = new javax.swing.JTextField();

jTextField2 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{null, null},

18012011054 4
NOSQL

{null, null},

{null, null},

{null, null}

},

new String [] {

"fname", "lname"

));

jTable1.addMouseListener(new java.awt.event.MouseAdapter()

{ public void mouseClicked(java.awt.event.MouseEvent evt)

{ jTable1MouseClicked(evt);

});

jScrollPane1.setViewportView(jTable1);

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jLabel1.setText("First Name:");

jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jLabel2.setText("Last Name:");

jTextField1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt)

{ jTextField1ActionPerformed(evt);

});

jTextField2.addActionListener(new java.awt.event.ActionListener()

{ public void actionPerformed(java.awt.event.ActionEvent evt)

{ jTextField2ActionPerformed(evt);

});

18012011054 5
NOSQL

jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jButton1.setText("Insert");

jButton1.addActionListener(new java.awt.event.ActionListener()

{ public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jButton2.setText("Update");

jButton2.addActionListener(new java.awt.event.ActionListener()

{ public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

});

jButton3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jButton3.setText("Load DB");

jButton3.addActionListener(new java.awt.event.ActionListener()

{ public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

});

jButton4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jButton4.setText("Delete");

jButton4.addActionListener(new java.awt.event.ActionListener()

{ public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

18012011054 6
NOSQL

});

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

jLabel3.setText("© By 18012011054");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignm

ent.LEADING) .addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(layout.createSequentialGroup()

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(18, 18, 18)

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 106,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
225, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(53, 53, 53)

.addComponent(jLabel3)

.addGap(0, 0, Short.MAX_VALUE))))

.addGroup(layout.createSequentialGroup()

.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 116,


javax.swing.GroupLayout.PREFERRED_SIZE)

18012011054 7
NOSQL

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

.addGroup(layout.createSequentialGroup()

.addGap(175, 175, 175)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE,
111, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(159, 159, 159)

.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE,
106, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()

.addGap(216, 216, 216)

.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 106,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
225, javax.swing.GroupLayout.PREFERRED_SIZE)))))

.addContainerGap(41, Short.MAX_VALUE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(143, 143, 143))

);

layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA

DING) .addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

18012011054 8
NOSQL

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(21, 21, 21)

.addComponent(jLabel3)

.addGap(68, 68, 68)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 26,


javax.swing.GroupLayout.PREFERRED_SIZE))))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(27, 27, 27)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)

.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 46,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68,
Short.MAX_VALUE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(67, 67, 67))

);

pack();

18012011054 9
NOSQL

}// </editor-fold> private void

jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

BasicDBObject document = new BasicDBObject();

document.put("fname",jTextField1.getText());

document.put("lname",jTextField2.getText());

table.insert(document); jTextField1.setText("");

jTextField2.setText("");

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

con_db();

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

int sindex = jTable1.getSelectedRow();

jTextField1.setText(tb.getValueAt(sindex,0).toString());

jTextField2.setText(tb.getValueAt(sindex,1).toString());

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

18012011054 10
NOSQL

BasicDBObject searchQuery = new BasicDBObject();

String name = jTextField1.getText();

searchQuery.put("fname",name);

table.remove(searchQuery);

jTextField1.setText("");

jTextField2.setText("");

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

BasicDBObject query = new BasicDBObject();

query.put("fname",jTextField1.getText());

BasicDBObject newDocument = new BasicDBObject();

newDocument.put("lname", jTextField2.getText());

BasicDBObject updateObj = new BasicDBObject();

updateObj.put("$set",newDocument);

table.update(query, updateObj);

jTextField1.setText("");

jTextField2.setText("");

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see


http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

18012011054 11
NOSQL

*/

try {

for(javax.swing.UIManager.LookAndFeelInfoinfo:javax.swing.UIManager

.getInstalledLookAndFeels()) {

if

("Nimbus".equals(info.getName()))

{ javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {


java.util.logging.Logger.getLogger(MongoDBViewer.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);

} catch (InstantiationException ex)


{ java.util.logging.Logger.getLogger(MongoDBViewer.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);

catch(IllegalAccessExceptionex)
java.util.logging.Logger.getLogger(MongoDBViewer.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(MongoDBViewer.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MongoDBViewer().setVisible(true);

});

18012011054 12
NOSQL

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTable jTable1;

private javax.swing.JTextField jTextField1;

private javax.swing.JTextField jTextField2;

// End of variables declaration

Features of the Application:-

18012011054 13
NOSQL

18012011054 14
NOSQL

18012011054 15
NOSQL

18012011054 16

You might also like