jdbc-hsqldb exception for text table - java

My java code is :
conn = DriverManager.getConnection(url, "user", "password"); // line 1
stm = conn.createStatement(); // line 2
stm.execute("CREATE TEXT TABLE someTableName("NLID" VARCHAR(20),
"Scheduled.Primary.Scripting.Code" VARCHAR(20), "Scheduled.Site" VARCHAR(20),
"Scheduled.Location.Long.Name" VARCHAR(20),
"primary_key_1644" int PRIMARY KEY)"); // line 3
stm.execute("SET TABLE someTableName SOURCE
"/some.csv;ignore_first=true;all_quoted=true;shutdown=true""); // line 4
The CSV which is being linked to the hsqldb is :
NLID,Scheduled.Primary.Scripting.Code,Scheduled.Site,Scheduled.Location.Long.Name,primary_key_1644
100,INMRSB,Shopping,Shopping General-Banner-728x90-INMRSB-I,1
100002,MSVT08,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 8-Streaming Media-300x60-MSVT08-S,2
100004,MSV10T,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 10-Streaming Media-300x60-MSV10T-S,3
I get the below exception in line 4 :
java.sql.SQLException: bad TEXT table source file - line number: 1196 java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" in statement [SET TABLE someTableName SOURCE "/some.csv;ignore_first=true;all_quoted=true;shutdown=true"]
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S"
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.TextTable.connect(Unknown Source)
at org.hsqldb.TextTable.openCache(Unknown Source)
at org.hsqldb.TextTable.setDataSource(Unknown Source)
at org.hsqldb.StatementCommand.getResult(Unknown Source)
at org.hsqldb.StatementCommand.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 6 more
Caused by: java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at org.hsqldb.rowio.RowInputText.readInteger(Unknown Source)
at org.hsqldb.rowio.RowInputBase.readData(Unknown Source)
at org.hsqldb.rowio.RowInputText.readData(Unknown Source)
at org.hsqldb.rowio.RowInputBase.readData(Unknown Source)
at org.hsqldb.rowio.RowInputText.readData(Unknown Source)
at org.hsqldb.RowAVLDiskData.getRowData(Unknown Source)
at org.hsqldb.persist.RowStoreAVLDiskData.get(Unknown Source)
... 14 more
Any help will be appreciable .
EDITED : Hsqldb throws NumberFormatException while reading a column which is defined as VARCHAR !!!
MODIFIED : The exception occurs because one of the column values has a comma in it like "Mind, Body Connection-Banner-728x90-HEAMBA-S" . Since the hsqldb reads the csv as a table so an additional comma is interpreted as an additional column by the hsql . Can anyone guide me how to circumvent this ?

As the OP found out, following these simple steps help find the problem in the CSV file:
Look at the error message: bad TEXT table source file - line number: 1196 The line number is the line number of the CSV file. Using a text editor, go to that line. Lines are numbered from 1.
ava.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" note NumberFormatException indicates a number was expected, instead the reported string was found.
Luckily, there is only one number (INT) column in the table, so it's easy to see there is an extra comma in the CVS, which terminates the last VARCHAR string and causes the problem.
The TEXT table definition includes all_quoted=true, which means if you use double quotes around the string that contains the comma, the whole string is treated as a field and the problem disappears.

Related

Is it possible to use print Strings and format doubles in the same printf statement? IllegalFormatConversionException

I want to print a sentence with formatted doubles without having to use several print statements. These are a couple of the statement I tried to run:
System.out.printf("( %.2f", real + ", %.2f", imaginary + ")");
System.out.printf("(" + "%.2f",real + ", " + "%.2f",imaginary + ")");
However, this is the error I get
java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at ComplexNumber.print(ComplexNumber.java:17)
at ComplexClient.main(ComplexClient.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Am I just using the printf statement incorrectly? Or is what I'm trying to do not possible? Thanks
You don't mix in the variables within the construction of your format string. You should create your format string as the first parameter, with all variables passed in afterwards. The number of variables passed in must match the number of placeholders in the format string, and they must match the formats specified by the placeholders.
Try:
// <- format ->
System.out.printf("( %.2f, %.2f)", real, imaginary);
// ... variables afterwards here
This matches the printf method signature, which takes in the format String first, followed by any number of Object variable parameters.

Escaping a json string inside a properties file

I use typical properties file in a java environment. I want one of the properties to be a json array:
#Country list in json by language
countries = [{"Name":"America"},{"Name":"Germany"},...]
This gives a number format exception in java when trying to read the string mapped to countries.
I tried a bunch of escaping sequences, but none seem to work:
countries = [{\"Name\":\"America\"},{\"Name\":\"Germany\"},...]
countries = [{\\"Name\\":\\"America\\"},{\\"Name\\":\\"Germany\\"},...]
countries = [{''Name'':''America''},{''Name'':''Germany''},...]
I'm wondering why a number formatexception is thrown, considering this is a string? Also, what is wrong with a json string that makes the file flip? Is it the [, {, " or : character(s)?
EDIT:
Her's the actual json in my properties file:
countries_json = [{"Name":"Afghanistan","Code":"AF","TelephoneCode":"+93"},{"Name":"Belgium","Code":"BE","TelephoneCode":"+32"}]
Here's the code in my jsp page which gets this value:
<input type="hidden" id="countryListJSON" value='<s:text name="countries_json"/>'/>
And here's the exception that happens on the java backend when the jsp is being rendered:
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: ""Name":"Afghanistan""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.text.MessageFormat.makeFormat(Unknown Source)
at java.text.MessageFormat.applyPattern(Unknown Source)
at java.text.MessageFormat.<init>(Unknown Source)
at com.opensymphony.xwork2.util.LocalizedTextUtil.buildMessageFormat(LocalizedTextUtil.java:704)
at com.opensymphony.xwork2.util.LocalizedTextUtil.getDefaultMessage(LocalizedTextUtil.java:663)
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:534)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:259)
at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:131)
at org.apache.struts2.util.TextProviderHelper.getText(TextProviderHelper.java:75)
at org.apache.struts2.components.Text.end(Text.java:160)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.shared.jsp.RegisterForm_jsp._jspx_meth_s_005ftext_005f14(RegisterForm_jsp.java:874)
at org.apache.jsp.shared.jsp.RegisterForm_jsp._jspService(RegisterForm_jsp.java:177)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
...
The struts tag might be treating it as a message argument. So its expecting a {0}, {1} etc. Give escape quotes for { in your property file like countries = ['{'"Name":"America"'}','{'"Name":"Germany"'}',...]

Look-behind group for a Java regular expression does not have an obvious maximum length near

I want to:
When I get a big string I need to find stuff in it using a regular expression in Java to separate using the following formulas:
If a line (after \n) has over 1000 characters, check if the 1000th character is enclosed in after an odd '.
Then add a concat string '\n||' between the 1000 and 1001 characters
If 1000 and 1001 characters are '' (escape character for plsql) then insert it between 1001 and 1002
Anyway I made this regular expression:
"\n(?<kiloCharacters>[^\n]{1000})(?<=(?<newLine>\n)(?<pairsAndText>[^'\n]{0,1001}|[^\n']{0,1001}'[^\n']{0,1001}'[^\n']{0,1001}){0,1001}(?<oddComa>')(?<text>[^\n']{0,1001}))(?(?<=')(?!'))"
Let me explain it:
"\n(?<kiloCharacters>[^\n]{1000}) --> Newline and 1000 characters
(?<= --> Let's look behind to check if we have an odd number of '
(?<newLine>\n) --> Start from new line
(?<pairsAndText> --> All pairs of '
[^'\n]{0,1001} --> Eighter 0 '
| --> or
[^\n']{0,1001}'[^\n']{0,1001}'[^\n']{0,1001}){0,1001} --> (text* ' text* ' text* )*
(?<oddComa>') --> Last comma
(?<text>[^\n']{0,1001}) --> Text after that comma
) --> End of actual looking behind
(?(?<=')(?!'))" --> This part check if we are inside an escaped character '' as we can not concat stuff between here
Anyway, it seems I get the folowing error.
Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 161
(?<kiloCharacters>[^
]{1000})(?<=(?<newLine>
)(?<pairsAndText>[^'
]{0,1001}|[^
']{0,1001}'[^
']{0,1001}'[^
']{0,1001}){0,1001}(?<oddComa>')(?<text>[^
']{0,1001}))(?(?<=')(?!'))
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
Why does it do that? Did I not make the limitation by using {0,1001} instead of *?
Java's regex engine does not support variable length look behind. That means that when the length of the look behind is not fixed the engine will throw this exception. Your look behind's length is variable, thus you get this exception.
Java regex error - Look-behind group does not have an obvious maximum length

How to read out a column with a colon (:) in its name in hibernate and postgresql

I am given a postgre database in which I have a table that has a column called:
addr:city
Since I cant use colons in java classvariables I tried to represent it this way:
#Column(name = "addr:city")
private String addrCity;
Sadly this didn't work, I get the following exception (I am sorry for the german language in the exception text, I don't know why it is this way - translated it means a Syntax error at the : (colon)):
ERROR: FEHLER: Syntaxfehler bei »:« Position: 52 Exception in thread
"main" org.hibernate.exception.SQLGrammarException: FEHLER:
Syntaxfehler bei »:« Position: 52 at
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at
org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy12.executeQuery(Unknown Source) at
org.hibernate.loader.Loader.getResultSet(Loader.java:1978) at
org.hibernate.loader.Loader.doQuery(Loader.java:829) at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2463) at
org.hibernate.loader.Loader.doList(Loader.java:2449) at
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279) at
org.hibernate.loader.Loader.list(Loader.java:2274) at
org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470) at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at
org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115) at
org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) at
database.BerlinPolygonDAO.getBerlinLocationByID(BerlinPolygonDAO.java:52)
at databaseAccess.Tester.main(Tester.java:33) Caused by:
org.postgresql.util.PSQLException: FEHLER: Syntaxfehler bei »:«
Position: 52 at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601) at
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 16 more
I read in some other posts that I should escape it the way below, but it did not work for me (exception that simply says that there is a syntax error with the "")
#Column(name = "addr\\:city")
private String addrCity;
It also didn't help to use two colons:
#Column(name = "addr::city")
private String addrCity;
Then I get an exception, that the column "addr" (so without the colon and the rest) doesn't exist.
Can anyone tell me what I need to do for reading out this column?
I found this related question that deals with the exact same column I'm dealing with now and the problem with the colon, but not in hibernate. I'm thinking if this might help somehow.
I solved my problem.
If there is a colon in a columname in postgresql and you try to read it out with hibernate you need to declare it like this:
#Column(name = "\"addr:city\"")
private String addrCity;

Velocity - Correct Regex to remove control characters?

I'm trying to remove undesirable characters from a string in Velocity (newlines are ok, but not things like EM and CAN ASCII control characters).
#set($cleanScreen = $cleanScreen.replaceAll("\p{Cc}", ""))
Throws:
org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 13, column 82. Encountered: "p" (112), after : "\"\\"
at org.apache.velocity.Template.process(Template.java:137)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:415)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1077)
at org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java:303)
at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:503)
and
#set($cleanScreen = $cleanScreen.replaceAll("[[:cntrl:]]", ""))
This one doesn't thrown an exception, instead, it matches the characters c,n,t,r,l and removes them from the string.
and...
#set($cleanScreen = $cleanScreen.replaceAll("\\p{Cntrl}", ""))
Throws:
java.util.regex.PatternSyntaxException: Illegal repetition near index 2
\\p{Cntrl}
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.closure(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at sun.reflect.GeneratedMethodAccessor168.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295)
at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245)
I've tried several regex's (many seem to work in Java, but not VTL)? My key issue seems to be how things differ in their escaping between Java and Velocity?
Can anyone help? I only have access the the VTL, not the Java class.
I can't comment on the actual regexp.
On the velocity side however, I find that...
#set($cleanScreen = $cleanScreen.replaceAll("\p{Cc}", ""))
#set($cleanScreen = $cleanScreen.replaceAll("[[:cntrl:]]", ""))
...these two are correct as they are. I have a little vtl shell into which I just copy pasted your vtl code. Are you really getting these errors with the first two expressions? How about using '\p{Cc}'?
#set($cleanScreen = $cleanScreen.replaceAll("\\p{Cntrl}", ""))
The '\\p' gets you into trouble.
On a side note, you can use http://velocity.apache.org/tools/devel/generic/EscapeTool.html for all your escaping needs.
Those Velocity Parser Exceptions, might come from the double-quotes characters.
I had similar problem in VTL when trying to String.replaceAll regular expression with a capturing group, like so:
#set( $Jira_links = $Jira_tickets.replaceAll("(CT-\d+)", "http://jira.site.com/browse/$1") )
Throws:
org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 2, column 58. Encountered: "d" (100), after : "\" (CT-\"
Changing it into single quotes worked:
#set( $Jira_links = $Jira_tickets.replaceAll('(CT-\d+)', 'http://jira.site.com/browse/$1') )

Categories