OrientDB java API with tinkerpop 3 - orientdb

what is the best API for Orient to Java for 3.0 snapshot?and how do I
1. connect
2. execute CRUD
3. execute native queries (SQL/Javascript).
Started with orientdb-gremlin but not yet upto speed, so wanted to check with community.
Thanks
Hari

This is an example of db connection and execution of an SQL query:
public static void main(String[] args) {
String DBname="Test";
String currentPath="remote:localhost/"+DBname;
OServerAdmin serverAdmin;
try {
serverAdmin = new OServerAdmin(currentPath).connect("root", "root");
if(serverAdmin.existsDatabase()){
OrientGraph g=new OrientGraph(currentPath);
Iterable<Vertex> result=g.command(new OCommandSQL("select from Person")).execute();
for(Vertex v:result){
String rid=v.getId().toString();
String name=v.getProperty("name");
String surname=v.getProperty("surname");
System.out.println(rid + " " + name + " " + surname);
}
g.shutdown();
}
serverAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hope it helps.
Regards,
Michela

Related

How to retry Spring Batch FlatFileItemReader?

i'm triyng to retry spring batch FlatFileItemReader but no success.
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
Resource resource = new FileSystemResource("input/1-101-D-2017-212-volume-per-transaction.csv");
try {
resource.contentLength();
} catch (IOException e) {
e.printStackTrace();
}
reader.setResource(resource);
reader.setRecordSeparatorPolicy(new BlankLineRecordSeparatorPolicy());
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
reader.setLineMapper(lineMapper);
reader.setStrict(false);
reader.setLinesToSkip(NUMBER_OF_HEADER_LINES);
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
reader.setSkippedLinesCallback(line -> tokenizer.setNames(line.split(",")));
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(new TransactionFieldSetMapper());
reader.setLineMapper(lineMapper);
return reader;
then into my step i have
.faultTolerant()
.retryLimit(3)
.retry(FileNotFoundException.class)
can someone give my some hint how can retry it?
Hopefully it's still useful for you, please refer to this example where I put an example of the retry reader using FlatFileItemReader:
https://github.com/jeronimogalicia/batch-retry-flatfile-item-reader
Basically you have to annotate your application class with #EnableRetry and annotate your reader like this:
#Bean
#StepScope
#Retryable(include = { ItemStreamException.class }, maxAttempts = 5)
ItemReader<Player> loadRecordsReader() throws Exception {
String filePath = "src/main/resources/players.csv";
System.out.println("Loading records from " + filePath + " try " + counter);
FlatFileItemReader<Player> itemReader = new FlatFileItemReader<>();
itemReader.setResource(new FileSystemResource(filePath));
itemReader.setLinesToSkip(1);
//DelimitedLineTokenizer defaults to comma as its delimiter
DefaultLineMapper<Player> lineMapper = new DefaultLineMapper<>();
lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
lineMapper.setFieldSetMapper(new PlayerFieldSetMapper());
itemReader.setLineMapper(lineMapper);
itemReader.open(new ExecutionContext());
return itemReader;
}

Sql anywhere 12 connection string

I am trying to connect sql anywhere12 database to my .net project but while connecting to the database i am facing an error
"{"Invalid connection string. Error parsing connection parameter string\r\nParameter name: connectionString"}"
while running this code.
private void btncon_Click(object sender, EventArgs e)
{
string connetionString = null;
SAConnection connection;
SACommand command;
string sqla = null;
// connetionString = "Data Source=SQL Anywhere 12 Demo;Initial Catalog=Demo2;User ID=dba;Password=sql";
connetionString = "Driver={SQL Anywhere 12};ENG=demo12;UID=dba;PWD=sql;DBN=sql;LINKS=TCPIP(HOST=localhost)";
sqla = "INSERT INTO student(Rollnumber,Name) values('" + textBox1.Text + "','" + textBox2.Text + "')";
connection = new SAConnection(connetionString);
try
{
connection.Open();
command = new SACommand(sqla, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
actually i have no much knowledge about the sql anywhere 12 connection string please help me to solve this error

Unable to know how to link data between two tables

I'm learning RESTful webservices from javabrains website. Here there is a section named Comments and this is related to a message, But I'm unable to know How Can I link these both.
Below is my SQL Tables for Messages and comments.
Messages
Comments
Here Basically, both look pretty same(The table design), but the values differ. And I'm using the below method to send the data.
public Comment addComment(long messageId, Comment comment) throws Exception {
Properties properties = new Properties();
properties.load(getClass().getClassLoader().getResourceAsStream("/config.properties"));
String userName = properties.getProperty("user");
String password = properties.getProperty("pass");
String url = properties.getProperty("Sqldburl");
int key = 0;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection conn = DriverManager.getConnection(url, userName, password);
String query = "select count(*) from Comments";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
rs.next();
key = rs.getInt(1);
} catch (Exception e) {
System.out.println(e);
}
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection conn = DriverManager.getConnection(url, userName, password);
String query = "insert into Comments(id, message, author) values(?,?,?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, key);
ps.setString(2, comment.getMessage());
ps.setString(3, comment.getAuthor());
ps.executeQuery();
} catch (Exception e) {
System.out.println(e);
}
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection conn = DriverManager.getConnection(url, userName, password);
String query = "select * from Comments where messageId=?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setLong(1, messageId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
comment.setAuthor(rs.getString("Author"));
comment.setId(rs.getInt("Id"));
comment.setMessage(rs.getString("message"));
}
} catch (Exception e) {
System.out.println(e + "b3");
}
return comment;
}
After writing this code, I've realized that Here I'm adding a comment for sure into Comments table, But it is no where linked to the Messages.
I know a way, that is I've create a new column in the Comments table and using join operation, I need to update the same messageId in comments table. But I want to know if there is a better way of getting this done, without using the concept of joins.
In MessageBean, there is a map declared as below.
private Map<Long, Comment> comments = new HashMap<>();
#XmlTransient
public Map<Long, Comment> getComments() {
return comments;
}
public void setComments(Map<Long, Comment> comments) {
this.comments = comments;
}
can I take any advantage of this to avoid join?

Nokia Forms not showing text

I only wish Nokia documentation was more helpful. Its search on developer documentation totally sucks.
public class UpdateJourney extends Form implements CommandListener, Runnable {
private LocationProvider myLocation;
private Criteria myCriteria;
private Location myCurrentLocation;
private HomeScreen helloScreen;
private Command exitCommand;
private Thread getLocationThread = new Thread(this);;
public UpdateJourney(HomeScreen helloScreen) {
super("Taxeeta");
this.helloScreen = helloScreen;
getLocationThread.start();
}
public void run() {
myCriteria = new Criteria();
myCriteria.setHorizontalAccuracy(500);
try {
myLocation = LocationProvider.getInstance(myCriteria);
myCurrentLocation = myLocation.getLocation(60);
} catch (LocationException e) {
e.printStackTrace();
System.out
.println("Error : Unable to initialize location provider");
return;
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Error: Waited enough for location to return");
return;
}
System.out.println("Location returned Lat:"
+ myCurrentLocation.getQualifiedCoordinates().getLatitude()
+ " Lng:"
+ myCurrentLocation.getQualifiedCoordinates().getLongitude());
String helloText = new String("Location returned Lat:"
+ myCurrentLocation.getQualifiedCoordinates().getLatitude()
+ " Lng:"
+ myCurrentLocation.getQualifiedCoordinates().getLongitude());
super.append(helloText);
exitCommand = new Command("Location returned Lat:"
+ myCurrentLocation.getQualifiedCoordinates().getLatitude()
+ " Lng:"
+ myCurrentLocation.getQualifiedCoordinates().getLongitude(),
Command.EXIT, 1);
addCommand(exitCommand);
setCommandListener(this);
}
}
do you mean not showing from this command?:
System.out.println("Location returned Lat:"
+ myCurrentLocation.getQualifiedCoordinates().getLatitude()
+ " Lng:"
+ myCurrentLocation.getQualifiedCoordinates().getLongitude());
It's not showing to phone screen. Instead it will show on console (IDE / debugging).
to showing text on Form.. you need to use somethings like:
form.addComponent(new Label("hi...");
hope it helps.

Google web toolkits

Can anyone please suggest a website where I can find step by step instructions on how to add jdbc to a gwt project and how to access data from a database within a gwt project? I'm new to GWT and can't to find any good resources to learn from.
Thanks
GWT is a client side technology - it creates code that runs on a browser. You can not talk to databases directly from browser. You need an intermediary servlet server. Here is what you need to do:
Use GWT-RPC for your GWT code to talk to the servlet server. There are a lot of good tutorials around the web.
Create server-side code that uses JDBC to talk to your database. Ankit already provided you with a link to example: http://code.google.com/p/gwt-examples/wiki/project_MySQLConn
You can also take a direct route and use one of the pre-packaged frameworks that allow you to talk "directly" from GWT to database, where framework provides the intermediate step (GWT-RPC to JDBC): gwtexpress
Just follow the google's tutorial itself. GWT is a java framework which converts java code into javascript code on compilation. And please dont confuse with database connection and gwt application. They are totally independent. In gwt application you will be seeing client & server packages. Only classes inside client package will be compiled to javascript code. You have to write jdbc code inside the server package. These codes will not be (can't be) compiled into javascript.
To bring the database data into client side you have to make rpc call.
I hope this information helps you for your basic understanding.
In that code you can learn how to connect database in gwt with jdbc
public class ExampleServiceImpl extends RemoteServiceServlet implements ExampleService{
//private Connection con=null;
private String status;
private String url="jdbc:mysql://localhost:3306/test";
private String user="test";
private String pass = "123456";
private Person people;
private ResultSet resultSet=null;
private Statement stm=null;
private Connection con=null;
private Statement stm2=null;
private Connection conn2=null;
private ResultSet resultSet2=null;
private MySqlConnection conn=new MySqlConnection();
#Override
public Person getPerson(String name,String surname,int password) {
Person personinfo=new Person();
personinfo.setName(name);
personinfo.setSurname(surname);
personinfo.setPassword(password);
ResultSet resultSet=null;
Statement stm=null;
Connection con=null;
MySqlConnection conn=new MySqlConnection();
con = conn.getConnection();
try {
stm = ((Connection) con).createStatement();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
String sorgu = "SELECT * FROM person";
try {
resultSet = stm.executeQuery(sorgu);
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
while(true){
String sql = "INSERT INTO person " +
"VALUES ("+ password +", '" + name+ "','" + surname + "')";
try {
stm.executeUpdate(sql);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
((Connection) con).setAutoCommit(false);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
((Connection) con).commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return personinfo;
}
}
public class MySqlConnection extends RemoteServiceServlet {
private static final long serialVersionUID = 1L;
public static Connection con;
public static Connection getConnection()
{
try
{
if(con==null)
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mysqlconn?user=root&password=123456";
con= DriverManager.getConnection(url);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return con;
}
public static void CloseConnection()
{
try
{
con.close();
con = null;
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}