forked from agiamas/mongo-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.java
50 lines (34 loc) · 1.55 KB
/
blog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// blog.java
import java.sql.*;
import com.mongodb.jdbc.*;
public class blog {
static void print( String name , ResultSet res )
throws SQLException {
System.out.println( name );
while ( res.next() ){
System.out.println( "\t" + res.getInt( "num" ) + "\t" + res.getString( "title" ) + "\t" + res.getObject( "tags" ) );
}
}
public static void main( String args[] )
throws SQLException , ClassNotFoundException {
Class.forName( "com.mongodb.jdbc.MongoDriver" );
Connection c = DriverManager.getConnection( "mongodb://localhost/exampledb" );
MongoConnection mc = (MongoConnection)c;
Statement st = c.createStatement();
st.executeUpdate( "drop table blogposts" );
PreparedStatement ps = c.prepareStatement( "insert into blogposts ( title , tags , num ) values ( ? , ? , ? )" );
ps.setString( 1 , "first post!" );
ps.setObject( 2 , new String[]{ "fun" , "eliot" } );
ps.setInt( 3 , 1 );
ps.executeUpdate();
ps.setString( 1 , "wow - this is cool" );
ps.setObject( 2 , new String[]{ "eliot" , "bar" } );
ps.setInt( 3 , 2 );
ps.executeUpdate();
ps.close();
System.out.println( mc.getCollection( "blogposts" ).find().toArray() );
print( "num should be 1 " , st.executeQuery( "select * from blogposts where tags='fun'" ) );
print( "num should be 2 " , st.executeQuery( "select * from blogposts where tags='bar'" ) );
// TODO indexing
}
}