-
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class ModelJTree extends JFrame {
private JTree tree;
private DefaultTreeModel model;
private DefaultMutableTreeNode rootNode,Medievel,Ancient,Philosophers;
public ModelJTree() {
DefaultMutableTreeNode philosophersNode = getPhilosopherTree();
model = new DefaultTreeModel(philosophersNode);
tree = new JTree(model);
JButton addButton = new JButton("Add Philosopher");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
addPhilosopher();
}
});
JButton removeButton = new JButton("Remove Selected Philosopher");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
removeSelectedPhilosopher();
}
});
JPanel inputPanel = new JPanel();
inputPanel.add(addButton);
inputPanel.add(removeButton);
Container container = getContentPane();
container.add(new JScrollPane(tree), BorderLayout.CENTER);
container.add(inputPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
}
private void addPhilosopher() {
DefaultMutableTreeNode parent = getSelectedNode();
System.out.println(parent);
if (parent == null) {
JOptionPane.showMessageDialog(ModelJTree.this, "Select an area.", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
String name = JOptionPane.showInputDialog(ModelJTree.this, "Enter Name:");
model.insertNodeInto(new DefaultMutableTreeNode(name), parent, parent.getChildCount());
}
private void removeSelectedPhilosopher() {
DefaultMutableTreeNode selectedNode = getSelectedNode();
if (selectedNode != null)
model.removeNodeFromParent(selectedNode);
}
private DefaultMutableTreeNode getSelectedNode() {
return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
}
private DefaultMutableTreeNode getPhilosopherTree() {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Philosophers");
DefaultMutableTreeNode ancient = new DefaultMutableTreeNode("Ancient");
rootNode.add(ancient);
ancient.add(new DefaultMutableTreeNode("Socrates"));
DefaultMutableTreeNode medieval = new DefaultMutableTreeNode("Medieval",true);
rootNode.add(medieval);
return rootNode;
}
public static void main(String args[]) {
new ModelJTree();
}
}
">Hi....My question is In a JTree i want to add nodes to only the nodes whose parent is root node.Please help me with this.In the following code it should be possible to add node to "Medieval" but not "socrates.import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;public class ModelJTree extends JFrame { private JTree tree; private DefaultTreeModel model; private DefaultMutableTreeNode rootNode,Medievel,Ancient,Philosophers; public ModelJTree() { DefaultMutableTreeNode philosophersNode = getPhilosopherTree(); model = new DefaultTreeModel(philosophersNode); tree = new JTree(model); JButton addButton = new JButton("Add Philosopher"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { addPhilosopher(); } }); JButton removeButton = new JButton("Remove Selected Philosopher"); removeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { removeSelectedPhilosopher(); } }); JPanel inputPanel = new JPanel(); inputPanel.add(addButton); inputPanel.add(removeButton); Container container = getContentPane(); container.add(new JScrollPane(tree), BorderLayout.CENTER); container.add(inputPanel, BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); } private void addPhilosopher() { DefaultMutableTreeNode parent = getSelectedNode(); System.out.println(parent); if (parent == null) { JOptionPane.showMessageDialog(ModelJTree.this, "Select an area.", "Error", JOptionPane.ERROR_MESSAGE); return; } String name = JOptionPane.showInputDialog(ModelJTree.this, "Enter Name:"); model.insertNodeInto(new DefaultMutableTreeNode(name), parent, parent.getChildCount()); } private void removeSelectedPhilosopher() { DefaultMutableTreeNode selectedNode = getSelectedNode(); if (selectedNode != null) model.removeNodeFromParent(selectedNode); } private DefaultMutableTreeNode getSelectedNode() { return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); } private DefaultMutableTreeNode getPhilosopherTree() { DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Philosophers"); DefaultMutableTreeNode ancient = new DefaultMutableTreeNode("Ancient"); rootNode.add(ancient); ancient.add(new DefaultMutableTreeNode("Socrates")); DefaultMutableTreeNode medieval = new DefaultMutableTreeNode("Medieval",true); rootNode.add(medieval); return rootNode; } public static void main(String args[]) { new ModelJTree(); }}
-
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.81.7.169:1521:PTS", "TESTPTS", "TESTPTS");
System.out.println("connection established");
PreparedStatement ps = conn.prepareStatement("insert into DEMOBLOB values(?)");
System.out.println("insert executed");
File photo= new File("D:/Thinkingjava.pdf");
System.out.println("image loaded ");
FileInputStream photostream= new FileInputStream(photo);
System.out.println("photo taken");
ps.setBinaryStream(1,photostream,(int)photo.length());
ps.executeUpdate();
photostream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
above is the code which iam using and below is the exception i am getting
ORA-01460: unimplemented or unreasonable conversion requested
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:114)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:542)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1311)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:738)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:1313)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1232)
at oracle.jdbc.driver.OracleStatement.doExecuteWithBatch(OracleStatement.java:1353)
at oracle.jdbc.driver.OracleStatement.doExecute(OracleStatement.java:1760)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1807)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:332)
at PDFConnect.main(PDFConnect.java:24)">When iam tring to insert a pdf of 2mb in to oracle database.This is my source code.import java.io.File;import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;public class PDFConnect { public static void main(String args[]) { try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.81.7.169:1521:PTS", "TESTPTS", "TESTPTS"); System.out.println("connection established"); PreparedStatement ps = conn.prepareStatement("insert into DEMOBLOB values(?)"); System.out.println("insert executed"); File photo= new File("D:/Thinkingjava.pdf"); System.out.println("image loaded "); FileInputStream photostream= new FileInputStream(photo); System.out.println("photo taken"); ps.setBinaryStream(1,photostream,(int)photo.length()); ps.executeUpdate(); photostream.close(); } catch(Exception e) { e.printStackTrace(); } } }above is the code which iam using and below is the exception i am gettingORA-01460: unimplemented or unreasonable conversion requested at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:114) at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208) at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:542) at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1311) at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:738) at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:1313) at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1232) at oracle.jdbc.driver.OracleStatement.doExecuteWithBatch(OracleStatement.java:1353) at oracle.jdbc.driver.OracleStatement.doExecute(OracleStatement.java:1760) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1807) at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:332) at PDFConnect.main(PDFConnect.java:24)
-
-
-
-
-
-
-
-
-
-
Servlet Request Object
What is difference b/w WITH ServletRequest Object:................RequestDispatcher rd = getServletContext().getRequestDispatcher( "/servlet/XYZServlet"); rd.forward(request, response);with ServletContext Object:.......... RequestDispatcher rd = getServletContext().getRequestDispatcher( "/servlet/XYZServlet"); rd.forward(request, response);what is difference b/w these two objects?which one is better...
-
-
-
-
-
-
Difference between forward and sendRedirect
What is difference between forward and sendRedirect
-
-
J2EE Interview Questions
Sub Category
Questions
Answers
Last Updated
Ans