I need to store base64 encoded document in Sybase database via stored procedure. I use JTDS driver.
As long as I am not trying to store my PDF document everything is fine. However as soon I attempt to path encoded string. I get the following error
java.sql.SQLException: Sybase does not support char parameters > 255
bytes.
Here is the code
conn.setAutoCommit(false);
cs = conn.prepareCall("{call sp_save_pdf (?,?,?,?)}");
cs.setString(++i, id);
cs.setString(++i, source);
cs.setString(++i, base64EncodedDocument);
cs.registerOutParameter(++i, java.sql.Types.INTEGER);
cs.execute();
When Sybase Developers execute this procedure but they use JConnect driver. Here is my question does JTDS driver not support usage of long strings and I need to use JConnect or do I miss something in my code?
Thanks
Issue was with JTDS driver as soon as I switched to JConnect driver everything began to work as expected.
Related
Running Netcool 7.3.1. Looking for simple api to access Object Server Tables. I've already done the run an SQL command from nco_sql, and scraped the output into a C# data table, but wondering if there was some type of api that I could use for either C# or Java to access table data?
If you can use a more up-to-date version of Omnibus, you can use the built-in HTTP / REST API.
http://www-01.ibm.com/support/knowledgecenter/SSSHTQ_8.1.0/com.ibm.netcool_OMNIbus.doc_8.1.0/omnibus/wip/api/concept/omn_api_http_overview.html?lang=en
You may need to use sybase database adapter so far I have used below three ways to query netcool object server:
Free TDS - This is free sybase client.
Jconn3 - this is paid sybase client, but if you are using WebGUI/Impact, this driver comes with TIP.
nco_sql - here you may need to create a file with query and then pass it to nco_sql. This require extra effort to parse column wise information as output will be on console.
I prefer jconn3, simple and similar to jdbc driver, you only need this jar in classpath.
You can write your own java program to connect to Objectserver by simply initiating
//Load Sybase Driver
Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
String url = "jdbc:sybase:Tds:" + host + ":" + port;
con = DriverManager.getConnection(url, user, pass);
Execute Statements
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("Select count(*) from alerts.status");;`
I need to save some uni-code characters in Sql server 2005 DB with JDBC, When i try to save "O’CON" into DB column having type NVarchar using a stored procedure it saves "O?CON" `(where ’ character is not `` or ')
I did some R&D and found that i need to add useUnicode=true&characterEncoding=UTF-8 some where in my DBcon.properties file that contains all the details of DB connection,
Can some one help me out where to add this into properties file or it i can add it at run time when i create a connection object.
Or if someone can help me how to save unicodes characters in DB using java.
Thanks.
To connect to database you use connect string specific to your database and driver.
I used MS MSQ long time ago but I remember that they had jtds driver. Connect string for such driver looks like:
jdbc:jtds:sqlserver://hostname:1433/my_database;useUnicode=true;characterEncoding=UTF-8
But from that time MS create their own JDBC driver and if you use it then I think you can add:
sendStringParametersAsUnicode=true
to your connect string. It is decribed at http://technet.microsoft.com/en-us/library/ms378988.aspx
In a case of problems show us your connect string and code where it is used.
I am working with postgresql procedures and trying to call a procedure from my JDBC program. But getting runtime exception saying procedure doesn't exist eventhough I cross-checked and verified that the procedure name is correct.
This is what I am doing
CallableStatement cs = connection.prepareCall("{call proc1()}");
cs.executeUpdate();
And here's my proc1 procedure
create or replace procedure proc1()
as
begin
insert into employee_info values(1,'johnny','1111',43);
-----
end
This is what the output is
Connection Failed! ERROR: function proc1() does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
I dont understand why its not working eventhough proc1() exists in database.
And what should i Cast?
Add correct schema name to callable statement and it shall work. Please refer to below code for example.
CallableStatement cs = connection.prepareCall("{call yoursSchema.proc1()}");
Finally, I got the solution. The main problem was with JDBC Driver which I downloaded from the official website. I was using the
postgresql driver. I dont know what's wrong with it but It seems like it is not supporting proedures. So I switched to EnterpriseDB(EDB) driver. Now the same program works fine and procedures are getting executed.
I just made these changes
1)Changing Driver
2)Changing Driver Class url from "org.postgresql.Driver" to "com.edb.Driver"
3)Dabase url "jdbc:postgresql://host:port/db to "jdbc:edb://host:port/db"
That's all. Now the procedures works too.
i want to ask the MYSQL an UTF-8 Query but it does not work fine . when i try the following query , the result comes up truly :
String query = "select * from Terms where Term = 'lol'";
but with the following query doesn't make a response :
String query = "select * from Terms where Term = 'خدابخش'";
where the
'خدابخش'
part is in Persian and UTF-8 .
note that the connection to the database is fine .
Chances are that you may need to set your character encoding in your JDBC connection. If you are using MySQL JDBC Connector you do it using the property characterEncoding. Somewhat like this:
jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8
You may want to read the reference on encoding and character sets in your connector JDBC documentation.
This is the one that mentions the use of characterEncoding for the MySQL JDBC Connector:
Connector JDBC: Using Character Sets and Unicode
One or more of the following is true:
The Java compiler, compiling your code, is set to read the source file with a different encoding in which the source file was actually stored. In other words, there is a discrepancy between the encoding that your editor uses, the encoding in which the file is actually saved, and the encoding with which the Java compiler is reading your source code.
Your database isn't set correctly to accept/store Unicode characters. Ensure that your database is set correctly. Looks like you're using MySQL. You may want to create a dump of the database using mysqldump and witness how the database was created with respect to character sets.
I'm trying to insert an image file (.png,200KB) into Sql sever (columb type varbinary(max)) via jdbc type 4 (microsoft jdbc 3.0), here is my code:
crsi.moveToInsertRow();
crsi.updateInt(1, Integer.parseInt(txt_TargetID.getText()));
crsi.updateBinaryStream(2, fis,f.length());
crsi.updateString(3, txt_Name.getText());
crsi.updateString(4, btng_Gender.getSelection().getActionCommand());
crsi.updateString(5, dpk_Birthdate.getSelectedDateAsText());
crsi.updateString(6, txt_IdenNo.getText());
crsi.updateString(7, dpk_RecordDate.getSelectedDateAsText());
crsi.insertRow();
crsi.moveToCurrentRow();
crsi.acceptChanges();
crsi is cachedrowsetimpl object,fis is Fileinputstream object
Every going right, the columns get inserted, except the image column remain NULL.
What going wrong?
Ok, i've solved this question. I should have enabled FILESTREAM in SQL server , also must have a Filestream group database,in which must have rowguiid and uniqueindentifier column.
Furthermore i can't use cachedrowset to upload filestream but i must use preparestatement to upload filestream.(cachedrowset will cause sync conflict)