我有一个applet,它是一个注册表单。我在注册表单中填写数据,然后单击提交按钮调用neo4j数据库服务器,创建输入数据的节点。我这样做是通过java。< / p>
代码如下
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.io.*;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;
import org.neo4j.kernel.InternalAbstractGraphDatabase;
import org.neo4j.kernel.StoreLocker;
import org.neo4j.cypher.javacompat.*;
import java.util.Collection;
public class formModified extends JApplet implements ActionListener
{
JFrame frame=new JFrame();
String Firstname;
String Lastname,Birthdate,Gender,State,City,HobbyS,EducationS,OccupationS;
Object Hobby,Education,Occupation;
String HobbyList[]={"Select ANY ONE","Cricket","Hockry","Golf","BasketBall","Football"};
String EducationList[]={"Select ANY ONE","BSc","BCom","BE","B.Tech","MBA","MBBS","PhD"};
String OccupationList[]={"Select ANY ONE","Business","Service","others"};
JPanel panel=new JPanel(new SpringLayout());
JLabel l1=new JLabel("First Name",JLabel.LEFT);
JTextField t1=new JTextField(50);
JLabel l2=new JLabel("Last Name",JLabel.LEFT);
JTextField t2=new JTextField(50);
JLabel l3=new JLabel("Birthday",JLabel.LEFT);
JTextField t3=new JTextField(20);
JRadioButton r1=new JRadioButton("Male");
JRadioButton r2=new JRadioButton("Female");
JLabel l4=new JLabel("State",JLabel.LEFT);
JTextField t4=new JTextField(50);
JLabel l5=new JLabel("City",JLabel.LEFT);
JTextField t5=new JTextField(50);
JLabel l6=new JLabel("Hobby",JLabel.LEFT);
JComboBox jl1=new JComboBox(HobbyList);
JLabel l7=new JLabel("Education",JLabel.LEFT);
JComboBox jl2=new JComboBox(EducationList);
JLabel l8=new JLabel("Occupation",JLabel.LEFT);
JComboBox jl3=new JComboBox(OccupationList);
JLabel l9=new JLabel("THANK YOU" ,JLabel.LEFT);
JButton submit=new JButton("SUBMIT");
JLabel l10=new JLabel("stackTrace" ,JLabel.LEFT);
JTextArea tExtra=new JTextArea();
public void init()
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
void createAndShowGUI()
{
add(panel);
panel.setLayout(new SpringLayout());
panel.add(l10);
panel.add(tExtra);
panel.add(l1);
panel.add(t1);
panel.add(l2);
panel.add(t2);
panel.add(l3);
panel.add(t3);
// add(cpanel);
panel.add(r1);
panel.add(r2);
panel.add(l4);
panel.add(t4);
panel.add(l5);
panel.add(t5);
panel.add(l6);
panel.add(jl1);
panel.add(l7);
panel.add(jl2);
panel.add(l8);
panel.add(jl3);
panel.add(l9);
panel.add(submit);
/*panel.add(c1);
panel.add(c2);
panel.add(c3);
panel.add(c4);*/
/*panel.add(c2);*/
Dimension d =new Dimension(20,20);
Dimension d1=new Dimension(1000,1000);
l1.setLabelFor(t1);
l2.setLabelFor(t2);
t1.setPreferredSize(d);
t1.setSize(d);
t1.setMaximumSize(d);
// t1.setMinimumSize(d);
t2.setPreferredSize(d);
t2.setSize(d);
t2.setMaximumSize(d);
t3.setPreferredSize(d);
t3.setSize(d);
t3.setMaximumSize(d);
t4.setPreferredSize(d);
t4.setSize(d);
t4.setMaximumSize(d);
t5.setPreferredSize(d);
t5.setSize(d);
t5.setMaximumSize(d);
/*tExtra.setPreferredSize(d1);
tExtra.setSize(d1);
tExtra.setMaximumSize(d1);
tExtra.setLineWrap(true);*/
SpringUtilities.makeCompactGrid(panel,11,2,20,20,10,10);
submit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
Firstname=t1.getText();
Lastname=t2.getText();
Birthdate=t3.getText();
if(r1.isSelected())
{Gender=r1.getText();
}
else
Gender=r2.getText();
State=t4.getText();
City=t5.getText();
Hobby=jl1.getSelectedItem();
Education=jl2.getSelectedItem();
Occupation=jl3.getSelectedItem();
HobbyS=Hobby.toString();
EducationS=Education.toString();
OccupationS=Occupation.toString();
//JOptionPane.showMessageDialog( frame,"BEFORE FUNCTION CALL");
callFunction();
//JOptionPane.showMessageDialog( frame,"After FUNCTION CALL");
}
catch(Exception e)
{
JOptionPane.showMessageDialog( frame,"in action Catch bLOCK");
}
//Person.getFname(Firstname);
//System.out.println(Firstname)
}
public void callFunction()
{
JOptionPane.showMessageDialog( frame,"IN FUNCTION");
String test;
String test1;
String test2;
String test3;
String test4;
JOptionPane.showMessageDialog( frame," CREATE OBJECT NOW");
EmbeddedNeo4j hello = new EmbeddedNeo4j();
JOptionPane.showMessageDialog( frame,"OBJECT CREATED");
test2=hello.start() ;//starts database? connects to it?
JOptionPane.showMessageDialog( frame,"Connected to Database");
test=hello.createDb(Firstname,Lastname,Gender,State,City,HobbyS,EducationS,OccupationS);
JOptionPane.showMessageDialog( frame,test);
//hello.clearDb();
//JOptionPane.showMessageDialog( frame,"performing clustering");
test3=hello.shutDown();
JOptionPane.showMessageDialog( frame,test3);
}
public void start()
{
SecurityManager sm = new MySecurityManager();
System.setSecurityManager(sm);
}
public void stop()
{
}
public void paint(Graphics g)
{
// simple text displayed on applet
g.setColor(Color.red);
super.paint(g);
}
}
class EmbeddedNeo4j
{
String k="";
final String DB_PATH = "C://neo4j-community-2.0.1//data//graph.db";
String greeting;
String greeting1;
GraphDatabaseService graphDb;
Node firstNode;
Node adNode;
Label myLabel,myLabel1;
String start()
{
try
{
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH);
return k;
}
catch(Exception e)
{
k=Arrays.toString(e.getStackTrace());
System.out.println(k);
return k;
}
}
String verify(String fn)
{
String fn1=fn;
return fn1;
}
/* public static void main( final String[] args )
{
EmbeddedNeo4j hello = new EmbeddedNeo4j();
hello.createDb();
//.removeData();
hello.shutDown();
}*/
//
String createDb(String Firstname,String Lastname,String Gender,String State,String City,String Hobby,String Education,String Occupation)
//String createDb()
{
Transaction tx=null;
try
{
tx= graphDb.beginTx();
firstNode = graphDb.createNode();
myLabel = DynamicLabel.label("Users");
firstNode.addLabel(myLabel);
firstNode.setProperty( "firstName", Firstname );
firstNode.setProperty( "LastName", Lastname );
firstNode.setProperty( "Age", "21");
firstNode.setProperty( "Gender", Gender );
firstNode.setProperty( "State", State );
firstNode.setProperty( "City", City );
firstNode.setProperty( "Hobby", Hobby );
firstNode.setProperty( "Education", Education );
firstNode.setProperty( "Occupation", Occupation );
//secondNode = graphDb.createNode();
//secondNode.setProperty( "message", "World!" );
// relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
//relationship.setProperty( "message", "brave Neo4j " );
// END SNIPPET: addData
// START SNIPPET: readData
System.out.print( firstNode.getProperty( "firstName"));
System.out.print( firstNode.getProperty( "LastName" ));
System.out.print( firstNode.getProperty( "Age" ) );
System.out.print( firstNode.getProperty( "Gender") );
System.out.print( firstNode.getProperty( "State" ) );
System.out.print( firstNode.getProperty( "City") );
System.out.print( firstNode.getProperty( "Hobby") );
System.out.print( firstNode.getProperty("Education") );
System.out.print( firstNode.getProperty( "Occupation") );
// END SNIPPET: readData
greeting = ( (String) firstNode.getProperty( "firstName" ) )
+ ( (String) firstNode.getProperty( "LastName" ))
+ ( (String) firstNode.getProperty( "Age" ))
+ ( (String) firstNode.getProperty( "Gender" ))
+ ( (String) firstNode.getProperty( "State" ))
+ ( (String) firstNode.getProperty( "City" ))
+ ( (String) firstNode.getProperty( "Hobby"))
+ ( (String) firstNode.getProperty( "Education" ))
+ ( (String) firstNode.getProperty( "Occupation" ));
// START SNIPPET: transaction
tx.success();
}
catch(Exception e)
{
tx.failure();
}
finally
{
tx.finish();
return greeting;
}
// END SNIPPET: transaction
}
String createAdDb(String Title,String Content,String Publisher)
{
Transaction tx=null;
try
{
tx= graphDb.beginTx();
//adNode = graphDb.createNode();
// myLabel = DynamicLabel.label("Person");
//firstNode.addLabel(myLabel);
adNode = graphDb.createNode();
myLabel1 = DynamicLabel.label("Ads");
adNode.addLabel(myLabel1);
adNode.setProperty( "Title", Title);
adNode.setProperty( "Content",Content);
adNode.setProperty( "Publisher",Publisher);
System.out.print( adNode.getProperty("Title"));
System.out.print( adNode.getProperty( "Content" ));
System.out.print( adNode.getProperty( "Publisher" ) );
greeting1 = ( (String) adNode.getProperty("Title" ) )
+ ( (String) adNode.getProperty( "Content" ))
+ ( (String) adNode.getProperty( "Publisher" ));
tx.success();
}
catch(Exception e)
{
tx.failure();
}
finally
{
tx.finish();
return greeting1;
}
}
void clearDb()
{
try
{
FileUtils.deleteRecursively( new File(DB_PATH) );
}
catch ( IOException e )
{
//throw new RuntimeException( e );
}
}
void removeData()
{
Transaction tx=null;
try
{
tx = graphDb.beginTx() ;
// firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
//secondNode.delete();
// END SNIPPET: removingData
tx.success();
}
catch(Exception e)
{
tx.failure();
}
finally
{
// START SNIPPET: removingData
// let's remove the data
tx.finish();
}
}
String clustering()
{
Transaction tx=null;
ExecutionEngine engine = new ExecutionEngine( graphDb );
ExecutionResult result;
try
{
tx=graphDb.beginTx();
//Transaction ignored = graphDb.beginTx();
//result = engine.execute( "start n=node(*) match(n) return n" );
tx.success();
return k;
}
catch(Exception e)
{
tx.failure();
k=Arrays.toString(e.getStackTrace());
System.out.println(k);
return k;
}
finally
{
tx.finish();
}
}
String shutDown()
{
//System.out.println();
// START SNIPPET: shutdownServer
graphDb.shutdown();
return "Shutting down database ..." ;
// END SNIPPET: shutdownServer
}
}
我的代码运行良好,并在使用ide Bluej从appletviewer运行时根据需要创建节点。 但是当我尝试在浏览器中运行代码时,applet不会创建neo4j数据库实例。“对象创建”消息对话框后面没有消息对话框。 此外,neo4j中的messages.log中没有堆栈跟踪也没有任何消息。
另外,我这样做是通过创建一个包含我的java程序和neo4j所需的所有jar文件的jar文件。我已经在jar文件中给出了条目pint,如上面提到的这个java代码。我已经用jar文件签名了jarsigner。
我正在使用xampp服务器运行此文件。
我的html代码如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Form</title>
</head>
<body>
<h1>Registration Form</h1>
<hr>
<applet code="formModified.class"
width=500
height=500
codebase="http://localhost"
archive="5last.jar"
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
>
<PARAM NAME = "Application-Name" VALUE = "reg">
<PARAM name="separate_jvm" value="true">
</applet>
<hr>
</body>
</html>
可能是什么问题? 我正在使用Neo4j 2.01社区版。
答案 0 :(得分:0)
不要使用小程序
使用标准的java webframework和普通的html表单,其中服务器后端连接到neo4j数据库服务器或嵌入式neo4j实例。
applet可能想在本地创建一个没有权限的数据库,你也必须提供所有neo4j jar,就像你在浏览器中注意到的那样
对于独立应用程序,请使用webstart或java-fx