How to get unique values in jList? - java

I have an excel database with some values like
PlotID Sl_no Species Gbh_cm
P1 1 Cassia fistula 13
P1 2 Lagerstroemia parviflora 41
P1 3 Lagerstroemia parviflora 59
P2 84 Shorea robusta 97
P2 85 Shorea robusta 101
P2 86 Shorea robusta 103
P2 87 Terminalia tomentosa 227
P2 88 Terminalia tomentosa 83
P2 89 Terminalia tomentosa 226
P2 90 Terminalia tomentosa 88
I want to display distinct values of species in my jList.When I run my code I get distinct values of all species except Terminalia tomentosa which is the last species of my excel database. This species come two times.
My code is:
sql1="select distinct Species from [Data$] where Plot_ID = ? ";
pst1=con.prepareStatement(sql1);
pst1.setString(1, tmp);
//pst1.setString(2, tmp);
rs5=pst1.executeQuery();
while(rs5.next())
{
species=rs5.getString("Species");
System.out.println(species);
dlm1.addElement(species);
}
listSPECIES.setModel(dlm1);

Related

How to get only characters from a file in java [duplicate]

This question already has answers here:
extract data column-wise from text file using Java
(2 answers)
Closed 4 years ago.
I have a file txt. This is the file:
Team P W L D F A Pts
1. Arsenal 38 26 9 3 79 - 36 87
2. Liverpool 38 24 8 6 67 - 30 80
3. Manchester_U 38 24 5 9 87 - 45 77
4. Newcastle 38 21 8 9 74 - 52 71
5. Leeds 38 18 12 8 53 - 37 66
6. Chelsea 38 17 13 8 66 - 38 64
7. West_Ham 38 15 8 15 48 - 57 53
8. Aston_Villa 38 12 14 12 46 - 47 50
9. Tottenham 38 14 8 16 49 - 53 50
How can I get only the name of teams? I tried to use the regex in the following way but don't work:
FileReader f;
f=new FileReader("file.txt");
BufferedReader b;
b=new BufferedReader(f);
s=b.readLine();
String[] name = s.split("\\w+");
for(int i=0;i<name.length;i++)
System.out.println(name[i]);
How do I solve? Thanks to everyone in advance!
FileReader f;
f=new FileReader("file.txt");
BufferedReader b;
b=new BufferedReader(f);
while(s=b.readLine()!=null){
Matcher name=Pattern.compile("(?<=\\d\\.\\s)\\S+").matcher(s);
if(name.find())
System.out.println(name.group());
}
here the regex (?<=\\d\\.\\s)\\S+ will match only the name after the serial no. Regex
If you want to read line by line and your file has structure as you presented. These code enable you to get clubs names.
File f = new File("file.txt");
Scanner sc = new Scanner(f);
sc.nextLine();
while (sc.hasNextLine()) {
String[] name = sc.nextLine().split("\\s+");
System.out.println(name[1]);
}
try to use replaceAll, find all word characters (a-zA-Z_) and replace them all with empty. this gives team name.
s=b.readLine();
s.replaceAll("[^a-zA-Z_]+","");
System.out.println(s);
Your string s is one line:
1. Arsenal 38 26 9 3 79 - 36 87
All you need to do is split by space and get second entry:
s.split(" ")[1]
RegEx is overkill here. Do this for each line and add the name to a list at each step.

OrientDB Lucene Index very slow

I have a database with 100.000+ records. I want to index them on two fields with Lucene, so I added the following index:
create index Book.search on Book (title,isbn) FULLTEXT ENGINE LUCENE
However, when I search on one of the fields using the following query:
select from Book where [title,isbn] LUCENE "android"
The query is taking a very long time, like it's doing a full table scan. If I use the explain plan, it also suggests that's it's doing that:
explain select from Book where [title,isbn] LUCENE "android"
Result:
{
"result": [
{
"#type": "d",
"#version": 0,
"documentReads": 80551,
"current": "#16:217944",
"documentAnalyzedCompatibleClass": 80551,
"recordReads": 80551,
"_memoryIndex": "isbn:\n\t'[61 6c 6c 61]':1: [(1)]\n\t'[63 6f 6d 70 6c 65 74 6f]':1: [(6)]\n\t'[63 6f 6e]':1: [(3)]\n\t'[63 6f 72 73 6f]':1: [(5)]\n\t'[65 64 69 74 69 6f 6e]':1: [(15)]\n\t'[67 75 69 64 61]':1: [(0)]\n\t'[69 6d 70 61 72 61 72 65]':1: [(8)]\n\t'[69 74 61 6c 69 61 6e]':1: [(14)]\n\t'[70 65 72]':1: [(7)]\n\t'[70 6f 63 6f]':1: [(12)]\n\t'[70 72 6f 67 72 61 6d 6d 61 72 65]':1: [(10)]\n\t'[70 72 6f 67 72 61 6d 6d 61 7a 69 6f 6e 65]':1: [(2)]\n\t'[72]':1: [(4)]\n\t'[74 65 6d 70 6f]':1: [(13)]\n\tterms=14, positions=14, memory=32.9 KB\ntitle:\n\t'[31 35 33 30 30 35 38 32 33 36]':1: [(0)]\n\tterms=1, positions=1, memory=32.9 KB\n\nfields=2, terms=15, positions=15, memory=66.6 KB",
"fetchingFromTargetElapsed": 17037,
"evaluated": 80551,
"user": "#5:0",
"tips": [
"Query 'SELECT FROM Book WHERE [title, isbn] LUCENE \"android\"' fetched more than 50000 records: to speed up the execution, create an index or change the query to use an existent index"
],
"elapsed": 17040.559,
"resultType": "collection",
"resultSize": 848,
"#fieldTypes": "documentReads=l,current=x,documentAnalyzedCompatibleClass=l,recordReads=l,fetchingFromTargetElapsed=l,evaluated=l,user=x,elapsed=f"
}
],
"warnings": [
"Query 'SELECT FROM Book WHERE [title, isbn] LUCENE \"android\"' fetched more than 50000 records: to speed up the execution, create an index or change the query to use an existent index"
],
"notification": "Query executed in 17.686 sec. Returned 1 record(s)"
}
What am I missing here?
From your explain there is no index involved. So yes it is doing the scan
From the picture of your indexes i saw that the fields are declared in this order [isbn,title]
This should solve it:
select count(1) from Book where [isbn,title] LUCENE "android"
I tried to replicate your problem with 96000 records.
I used OrientDb 2.1.12.
Class book
I inserted a book with title "android" and isbn "12345"
The query select from Book where [title,isbn] LUCENE "android"
it was performed quickly
With explain
{
"result": [
{
"#type": "d",
"#version": 0,
"documentReads": 1,
"fullySortedByIndex": false,
"documentAnalyzedCompatibleClass": 1,
"recordReads": 1,
"Book_search_totalHits": 1,
"luceneIndex": true,
"fetchingFromTargetElapsed": 16,
"indexIsUsedInOrderBy": false,
"score": 8.087625,
"current": "#12:140533",
"totalHits": 1,
"_memoryIndex": "isbn:\n\t'[31 32 33 34 35]':1: [(0)]\n\tterms=1, positions=1, memory=32.9 KB\ntitle:\n\t'[61 6e 64 72 6f 69 64]':1: [(0)]\n\tterms=1, positions=1, memory=32.9 KB\n\nfields=2, terms=2, positions=2, memory=66.5 KB",
"involvedIndexes": [
"Book.search"
],
"limit": -1,
"evaluated": 1,
"user": "#5:0",
"elapsed": 11.263393,
"resultType": "document",
"resultSize": 1,
"#fieldTypes": "documentReads=l,documentAnalyzedCompatibleClass=l,recordReads=l,fetchingFromTargetElapsed=l,score=f,current=x,involvedIndexes=e,evaluated=l,user=x,elapsed=f"
}
],
"notification": "Query executed in 0.042 sec. Returned 1 record(s)"
}
How many records do you have with the title that contains "android" ?
Are they more than 50000 ?

Store data into aerospike

I have some data which is to be stored into aerospike against a column
Suppose the incomming data is
["A", 1]
Now the first question is how to hold this data in Java.
I tried this.
ArrayList value = new ArrayList();
value.add(new String("A"));
value.add(new Integer(2));
When i try to write this data to aerospike using
AerospikeClient client = client.put(new WritePolicy(),
new Key("namespace", "set", "test"),
new Bin("binName", value) );
Then if i use AQL to query then i am seeing
| AC ED 00 05 73 72 00 13 6A 61 76 61 2E 75 74 69 6C 2E 41 72 72 61 79 4C 69 73 74 78 81 D2 1D 99 C7 61 9D 03 00 01 49 00 04 73 69 7A 65 78 70 00 00 00 02 77 04 00 00 00 02 73 72 00 11 6A 61 76 61 2E 6C 61 6E 67 2E 49 6E 74 65 67 65 72 12 E2 A0 A4 F7 81 87 |
Some HexaDecimal numbers
but when i try to store the Data into Aerospike using
AerospikeClient client = client.put(new WritePolicy(),
new Key("namespace", "set", "test"),
new Bin("binName", Value.getAsList(value)) );
Then firing query through AQL gives me
["A",1]
Which is and seems like the intended behaviour but when i use the Aerospike client to fetch the values and check their types
List<Object> ret = (List<Object>) client.get(new Policy(), key, "test").getValue("binName");
if(ret.get(0) instanceof Long){
System.out.println("Got instance of long");
}
Then i can see the print statement though Initially i sent Integer data.
Why is this happening, and can anyone tell me the any alternate solution to save an incomming data into aerospike say the data is
["A",1]
PS:Please support your answer with small code snippit
FOUND SOME INFO ON GITHUB
In reference to this link there is some function
which i am copy/pasting below
/**
* Write/Read ArrayList<Object> directly instead of relying on java serializer.
*/
private void testListComplex(AerospikeClient client, Parameters params) throws Exception {
console.info("Read/Write ArrayList<Object>");
Key key = new Key(params.namespace, params.set, "listkey2");
client.delete(params.writePolicy, key);
byte[] blob = new byte[] {3, 52, 125};
ArrayList<Object> list = new ArrayList<Object>();
list.add("string1");
list.add(2);
list.add(blob);
Bin bin = new Bin(params.getBinName("listbin2"), list);
client.put(params.writePolicy, key, bin);
Record record = client.get(params.policy, key, bin.name);
List<?> receivedList = (List<?>) record.getValue(bin.name);
validateSize(3, receivedList.size());
validate("string1", receivedList.get(0));
// Server convert numbers to long, so must expect long.
validate(2L, receivedList.get(1));
validate(blob, (byte[])receivedList.get(2));
console.info("Read/Write ArrayList<Object> successful.");
}
There is a comment that server converts number to long
Now i have a question. So does it mean for this type of Case integer cannot be stored?
If you check the Aerospike datatypes, you'll see that they only support 64-bit integers, which are long types in Java.
If you don't need to access this data on the server through Lua UDF scripts, then you can just save it as serialized blob data. The java driver already supports native serialization which is what you did in the first attempt. The AQL is just showing you the serialized bytes but you can read it back in the java client just fine.
Or you can store it as a json serialized string so that it's more compatible with other language drivers that you might use in the future.

Different data when printing and writing to file

There is a stream of data which is sent from server. I need to store this byte stream into a file. The problem is the data which I output to console and the one which I store in a file are different. Seems like there is a change in format of data when I stored in a file.
Here is the program:
try
{
System.out.println("My Address is "+serverSocket.getLocalSocketAddress());
Socket server = serverSocket.accept(); // return a new socket
System.out.println("Connected to client "+server.getRemoteSocketAddress());
inputStream = server.getInputStream();
in = new DataInputStream(inputStream);
out = new FileOutputStream("output.txt");
ArrayList<Byte> bytes = new ArrayList<Byte>();
int curi;
byte cur;
byte[] curBytes = null;
int length = 0;
System.out.println("Before while loop");
while((curi = in.read())!=-1 && count!=500)
{
System.out.println(count+" Reading some data");
//out.write(curi);
cur = (byte)curi;
bytes.add(cur);
curBytes = getPrimativeArray(bytes);
String curBytesString = new String(curBytes, "UTF-8");
count++;
}
int i=0;
for(byte b : bytes)
{
System.out.print(b+" ");
curBytes[i] = b;
i++;
}
out.write(curBytes);
server.close();
}
catch(IOException e)
{
e.printStackTrace();
}
What I print using System.out.print(b+" "); and the one I store in curBytes[] are the same thing. But when I compare the console and file output, they are different.
Console output: 0 0 113 -100 -126 -54 0 32 14 1 0 0 1 -58 60 54 0 3 63 -2 85 74 -81 -88 0 9 1 24 85 74 -81 -48 0 13 65 -113 85 74 -81 -88 0 12 125 -126 85 74 -81 -88 0 13 21 97 85 74 -81 -88 0 13 31 19 85 74 -81 -48 0 13 42 24 0 6 0 0 0 0 0 0 0 0 0 0 32 0 7 -100 0 -5 6 -128 0 -56 29 -127 23 112 -1 -1 0 0 64 0 1 -121 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 49 53 64 111 110 101 46 97 116 116 46 110 101 116 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 54 53 64 111 110 101 46 97 116 116 46 110 101 116 37 50 57 54 53 45 49 53 48 53 48 54 50 51 50 55 48 50 45 50 48 53 48 54 54 50 55 54 54 64 48 48 55 56 48 48 49 49 16 32 1 5 6 64 0 0 0 32 16 0 0 0 120 0 17 16 32 1 24 -112 16 1 46 2 0 0 0 0 0 0 0 6 1 -113 0 4 0 33 -64 -42 0 91 5 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -120 -28 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -44 -39 8 0 4 0 -56 0 0 1 11 3 85 74 -81 -88 0 9 1 24 8 0 5 0 0 0 0 0 0 3 85 74 -81 -88 0 13 31 19 8 0 1 0 -56 0 0 0 6 3 85 74 -81 -48 0 13 42 24 -64 34 4 24 9 89 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 98 114 97 110 99 104 61 122 57 104 71 52 98 75 50 57 48 45 48 48 55 56 48 48 49 49 45 48 48 48 102 45 52 52 49 57 55 49 52 48 51 3 85 74 -81 -88 0 12 -120 -28 127 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 114 101 99 101 105 118 101 100 61 50 48 48 49
File Output: ^#^#q<9c><82>Ê^# ^N^A^#^#^AÆ<6^#^C?þUJ¯¨^# ^A^XUJ¯Ð^#^MA<8f>UJ¯¨^#^L}<82>UJ¯¨^#^M^UaUJ¯¨^#^M^_^SUJ¯Ð^#^M*^X^#^F^#^#^#^#^#^#^#^#^#^# ^#^G<9c>^#û^F<80>^#È^]<81>^Wpÿÿ^#^##^#^A<87>^\sip:+14222099915#one.att.net^\sip:+14222099965#one.att.net%2965-150506232702-2050662766#00780011^P ^A^E^F#^#^#^# ^P^#^#^#x^#^Q^P ^A^X<90>^P^A.^B^#^#^#^#^#^#^#^F^A<8f>^#^D^#!ÀÖ^#[^E^H^# ^#È^#^#^#^O^CUJ¯¨^#^L<88>ä^H^# ^#È^#^#^#^O^CUJ¯¨^#^LÔÙ^H^#^D^#È^#^#^A^K^CUJ¯¨^# ^A^X^H^#^E^#^#^#^#^#^#^CUJ¯¨^#^M^_^S^H^#^A^#È^#^#^#^F^CUJ¯Ð^#^M*^XÀ"^D^X YSIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;branch=z9hG4bK290-00780011-000f-441971403^CUJ¯¨^#^L<88>ä^?SIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;received=2001
Please let me know at what step I'm making a mistake.
The other answers here tell you to use a PrintWriter or a FileWriter instead of the FileOutputStream but I'm fairly sure that this is not what you want.
Your problem is that you're writing raw bytes to a file and then reading it back as characters and comparing that to byte values represented as characters and then printed with System.out.
Let's take a look at what happens when you print a byte with the value 65 (or 01000001 in binary).
When you use System.out.print you will invoke PrintStream.print(int) with the integer value of 65 which will in turn print the characters 6 and 5 to the terminal.
When you use out.write you will invoke FileOutputStream.write(byte[]) which will write the bits 01000001 to the file.
Later, when you check the contents of the file your tool will try to interpret this byte as a character and it will most likely use the ASCII encoding to do so (even if you're using Unicode as your default encoding this is likely what will happen since Unicode is a superset of ASCII). This results in the character A being printed.
If you want to view the output file in a way similar to what you've printed with System.out.print you can use the following command on linux:
$ hexdump -e '/1 "%i "' <file>
Example:
$ cat /etc/issue
Ubuntu 12.04.5 LTS \n \l
$ hexdump -e '/1 "%i "' /etc/issue
85 98 117 110 116 117 32 49 50 46 48 52 46 53 32 76 84 83 32 92 110 32
92 108 10 *
My first answer was wrong, so I am editing this because I made the assumption that you could write out a string to the FileOutputStream, but I don't think that is the case. FileOutputStream is only used for byte streams, so you've got to stick to that format when writing out to the file.
If you hold the data in a buffer[array], and then write those bytes out to a file that you have created using the output stream, it should work. I found this document that might be helpful.
The main idea is that somewhere in your code, the byte array isn't getting written to the file correctly. Perhaps its just a matter of adding the close() method.
out.close();
server.close();
reading and writing files in java
Here is the section I found useful.
import java.io.*;
public class Test {
public static void main(String [] args) {
// The name of the file to create.
String fileName = "temp.txt";
try {
// Put some bytes in a buffer so we can
// write them. Usually this would be
// image data or something. Or it might
// be unicode text.
String bytes = "Hello theren";
byte[] buffer = bytes.getBytes();
FileOutputStream outputStream =
new FileOutputStream(fileName);
// write() writes as many bytes from the buffer
// as the length of the buffer. You can also
// use
// write(buffer, offset, length)
// if you want to write a specific number of
// bytes, or only part of the buffer.
outputStream.write(buffer);
// Always close files.
outputStream.close();
System.out.println("Wrote " + buffer.length +
" bytes");
}
catch(IOException ex) {
System.out.println(
"Error writing file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
The console (System.out) is a PrintWriter, while the file output is a FileOutputStream.
The basic difference between Stream and Writer: Streams are supposed to manipulate "raw data", like numbers taken directly from binary format, while writers are used to manipulate "human-readable data", transforming all the data you write.
For example, the 6 int is different from the 6 character. When you use a stream, you write directly the int, while with a writer, the data wrriten is transformed into the character.
Then, if you want your file output to be the same as your console output, do not use FileOutputStream, but instead, use FileWriter, and it's method write(String).
How to make this work:
1 - replace out = new FileOutputStream("output.txt"); by out = new FileWriter("output.txt");
2 - replace out.write(curBytes); by:
for (byte b : curBytes) {
out.write(b + " ");
}
I would suggest you use IOUtils.copy and use a BufferedReader
to wrap your InputStream.
The output stream should obviously be FileOutputStream
I hope this helps

Decode unknow charset text save by php

I have a some record in MySQL such as
Vận hành linh hoạt trong má»i Ä‘k giao thông
which in hex as
56 c3 a1 c2 ba c2 ad 6e 20 68 c3 83 c2 a0 6e 68 20 6c 69 6e 68 20 68
6f c3 a1 c2 ba c2 a1 74 20 74 72 6f 6e 67 20 6d c3 a1 c2 bb c2 8d 69
20 c3 84 e2 80 98 6b 20 67 69 61 6f 20 74 68 c3 83 c2 b4 6e 67 20
I dont know how PHP save it, but read it from Java MySQL Connector show some strange character. And I can make it show the origin text by
copy the text above --> Notepad++ - Encoding in ASCII --> Paste text
--> Encoding in UTF-8
the original text should be:
Vận hành linh hoạt trong mọi đk giao thông
I know the problem is PHP save incorrect text format, but is there a way to decode it correctly in Java?
Are you sure the hex is exactly correct? Here is what I did...
String MESS = "56 c3 a1 c2 ba c2 ad 6e 20 68 c3 83 c2 a0 6e 68 20 6c 69 6e 68 20 68 6f c3 a1 c2 ba c2 a1 74 20 74 72 6f 6e 67 20 6d c3 a1 c2 bb c2 8d 69 20 c3 84 e2 80 98 6b 20 67 69 61 6f 20 74 68 c3 83 c2 b4 6e 67 20";
String[] hexchars = MESS.split(" ");
byte[] buf = new byte[hexchars.length];
for (int i = 0; i < hexchars.length; i++) {
buf[i] = (byte) Integer.parseInt(hexchars[i], 16);
}
try {
String s1 = new String(buf, "UTF-8"); // First encode UTF-8
buf = s1.getBytes("cp1252"); // ...then translate to cp1252
s1 = new String(buf, "UTF-8"); // ...then back to UTF-8
System.out.println(s1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
And the printed result is:
Vận hành linh hoạt trong m�?i đk giao thông
Which is almost right. Except the decoding of mọi it is incorrect, which makes me suspect the hex that you provided may not be correct. If you are 100% sure it is correct, I can try a little more to decode it.
UPDATE:
Here are my further thoughts:
You need to find out what encoding MySQL itself (the database) is set to.
You need to find out what encoding PHP is set to
possibly in PHP.INI
possibly set in the HTML metadata for the page that populates the table.
You need to find out what if any encoding the PHP MySQL driver runs with
Only then will there be a possibility of setting the MySQL Connector/J to the right encoding, and then possibly applying a second conversion in Java.

Categories