-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample2Insert.java
More file actions
126 lines (112 loc) · 4.18 KB
/
Sample2Insert.java
File metadata and controls
126 lines (112 loc) · 4.18 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.*;
import java.sql.*;
import com.machbase.jdbc.*;
public class Sample2Insert
{
public static Connection connect()
{
Connection conn = null;
try
{
String sURL = "jdbc:machbase://localhost:5656/mhdb";
Properties sProps = new Properties();
sProps.put("user", "sys");
sProps.put("password", "manager");
Class.forName("com.machbase.jdbc.MachDriver");
conn = DriverManager.getConnection(sURL, sProps);
}
catch ( ClassNotFoundException ex )
{
System.err.println("Exception : unable to load machbase jdbc driver class");
}
catch ( Exception e )
{
System.err.println("Exception : " + e.getMessage());
}
return conn;
}
public static void main(String[] args) throws Exception
{
Connection conn = null;
Statement stmt = null;
String sql;
try
{
conn = connect();
if( conn != null )
{
System.out.println("machbase JDBC connected.");
stmt = conn.createStatement();
for(int i=1; i<10; i++)
{
sql = "INSERT INTO SAMPLE_TABLE VALUES (";
sql += (i - 5) * 6552;//short
sql += ","+ ((i - 5) * 429496728);//integer
sql += ","+ ((i - 5) * 922337203685477580L);//long
sql += ","+ 1.23456789+"e"+((i<=5)?"":"+")+((i-5)*7);//float
sql += ","+ 1.23456789+"e"+((i<=5)?"":"+")+((i-5)*61);//double
sql += ",'id-"+i+"'";//varchar
sql += ",'name-"+i+"'";//text
sql += ",'aabbccddeeff'";//binary
sql += ",'192.168.0."+i+"'";//ipv4
sql += ",'::192.168.0."+i+"'";//ipv6
sql += ",TO_DATE('2014-08-0"+i+"','YYYY-MM-DD')";//dt
sql += ")";
stmt.execute(sql);
System.out.println( i+" record inserted.");
}
String query = "SELECT d1, d2, d3, f1, f2, name, text, bin, to_hex(bin), v4, v6, to_char(dt,'YYYY-MM-DD') as dt from SAMPLE_TABLE";
ResultSet rs = stmt.executeQuery(query);
while( rs.next () )
{
short d1 = rs.getShort("d1");
int d2 = rs.getInt("d2");
long d3 = rs.getLong("d3");
float f1 = rs.getFloat("f1");
double f2 = rs.getDouble("f2");
String name = rs.getString("name");
String text = rs.getString("text");
String bin = rs.getString("bin");
String hexbin = rs.getString("to_hex(bin)");
String v4 = rs.getString("v4");
String v6 = rs.getString("v6");
String dt = rs.getString("dt");
System.out.print("d1: " + d1);
System.out.print(", d2: " + d2);
System.out.print(", d3: " + d3);
System.out.print(", f1: " + f1);
System.out.print(", f2: " + f2);
System.out.print(", name: " + name);
System.out.print(", text: " + text);
System.out.print(", bin: " + bin);
System.out.print(", hexbin: "+hexbin);
System.out.print(", v4: " + v4);
System.out.print(", v6: " + v6);
System.out.println(", dt: " + dt);
}
rs.close();
}
}
catch( SQLException se )
{
System.err.println("SQLException : " + se.getMessage());
}
catch( Exception e )
{
System.err.println("Exception : " + e.getMessage());
}
finally
{
if( stmt != null )
{
stmt.close();
stmt = null;
}
if( conn != null )
{
conn.close();
conn = null;
}
}
}
}