What is the purpose of setting a client character set when connecting to mySQL in PHP? - mysqli

Using mysqli I might do:
$mysqli = new mysqli(...);
$mysqli->set_charset("utf8");
I assume that $mysqli->get_charset gets the current setting which is otherwise set by this.
This is the character set used for the client-server connection (PHP to talk to the MySQL server) - not the collation on the database I believe?
What is the significance of this? What is it used for? If there is a chunk of utf8 encoded data being passed from PHP to the MySQL Server (say in an INSERT query) what difference will this setting make? If this setting was latin1 would it matter?
Should it be the same as the collation used in the database?

do you use notepadd++ ? if you can see the images in notepadd ++ in dependence of encode is show "í" ... mysql set_charset work at same
enter image description here
enter image description here

Related

Go database/sql - Issue Commands on Reconnect

I have a small application written in Go that connects to a PostgreSQL database on another server, utilizing database/sql and lib/pq. When I start the application, it goes through and establishes that all the database tables and indexes exist. As part of this process, it issues a SET search_path TO preferredschema,public command. Then, for the remainder of the database access, I do not have to specify the schema.
From what I've determined from debugging it, when database/sql reconnects (no network is perfect), the application begins failing because the search path isn't set. Is there a way to specify commands that should be executed when it reconnects? I've searched for an event that might be able to be leveraged, but have come up empty so far.
Thanks!
From the fine manual:
Connection String Parameters
[...]
In addition to the parameters listed above, any run-time parameter that can be set at backend start time can be set in the connection string. For more information, see http://www.postgresql.org/docs/current/static/runtime-config.html.
Then if we go over to the PostgreSQL documentation, you'll see various ways of setting connection parameters such as config files, SET commands, command line switches, ...
While the desired behavior isn't exactly spelled out, it is suggested that you can put anything you'd SET right into the connection string:
connStr := "dbname=... user=... search_path=preferredschema,public"
// -----------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and since that's all there is for configuring the connection, it should be used for every connection (including reconnects).
The Connection String Parameters section of the pq documentation also tells you how to quote and escape things if whatever preferredschema really is needs it or if you have to grab a value at runtime and add it to the connection string.

Default value of Statement QueryTimeout on DB2 V9.7.7?

I tried to find out like what is the default value of Statement QueryTimeout in DB2 V9.7.7???
I googled alot but did not get this specific information. I found the value is 0 in Db2V9.7.4. I am having hard time to find in verison 9.7.7.
Please help me out.
There is no default query timeout within the DB2 Server or DB2 Client – DB2 will allow a query to run until completion.
Applications can (optionally) set the QueryTimeout attribute to control this. If you are using .NET, you might also check to see if your application sets the CommandTimeout property.

What kind of int storage is this?

We have an Firebird database for a (very crappy) application, and the app's front end, but nothing in between (i.e. no source code).
There is a field in the database that is stored as -2086008209 but in the front-end represents as 63997.
Examples:
Database Front-End
758038959 44093
1532056691 61409
28401112 65866
-712038758 40712
936488434 43872
-688079579 48567
1796491935 39437
1178382500 30006
1419373703 66069
1996421588 48454
890825339 46313
-820234748 45206
What kind of storage is this? The aim for us here is to access the application's back-end data and bypass the front-end GUI alltogether, so I need to know how to decode this field in order to get appropriate values from it. It is stored as a int in FireBird (I don't know if FireBird has signed/unsigned ints, but this is showing as signed when we select it).
This is the definition of the field:
It is not, as far as I can tell, de-normalised. The generator GEN_CONTACTS_ID has 66241 against it, which at a glance looks accurate.
I work on with an application that stores bitmaps in integers (just don't ask), if you express them in that form do you something useful or consistant
My impression is that the problem is in the front end. If what is stored in the DB is -2086008209, then what is stored in the DB is -2086008209. To understand better how the application is manipulating the data, try storing other numbers in the DB and see how they are displayed.
Did you come to this realization through logging SQL? If you havent, you may serve yourself well by using the Firebird Trace API to get that SQL: http://www.firebirdfaq.org/faq95/. An easier tool to parse the Trace API is this commercial product: http://www.upscene.com/products.fbtm.index.php.
I've used these tools and other techniques (triggers etc,.) to find what an application is using/changing in the Database.
Of course, if the SQL statement is select * from table, then these tools would not help much.

How can I get my client application name to show up on zos from java?

This page says I can put "clientProgramName" as one of the connection parameters and it will show up on db2 as the correlation ID.
And I quote:
In a java.util.Properties value in the info parameter of a
DriverManager.getConnection call.
We're using z/OS. The z/OS version of DB2 seems a lot more limited in terms of this kind of stuff.
Setting the client program name in the params hash of the connect call seems to have no effect, and when I put it on the end of the connect string url like this (which it also says I can do):
jdbc:db2://localhost:5036/DBNAME:clientProgramName=myprog
I get this error:
[jcc][10165][10051][4.11.77] Invalid database URL syntax:
jdbc:db2://localhost:5036/DBNAME:clientProgramName=myprog.
ERRORCODE=-4461, SQLSTATE=42815
Is there any way to send a custom user string to a z/OS db2 server so that connection can be identified on the server?
Depending on the method you use to connect to DB2, you use:
Class.forName
Class.forName("com.ibm.db2.jcc.DB2Driver");
Properties props = new Properties();
props.put("user", "scott");
props.put("password", "tiger");
props.put("clientProgramName", "My Program 1");
Connection conn = DriverManager.getConnection(
"jdbc:db2://localhost:50000/sample", props);
DataSource
Connection conn = null;
DB2SimpleDataSource ds = new com.ibm.db2.jcc.DB2SimpleDataSource();
ds.setDriverType(4);
ds.setServerName("localhost");
ds.setPortNumber(50000);
ds.setDatabaseName("sample");
ds.setUser("scott");
ds.setPassword("tiger");
ds.setClientProgramName("My Application 2");
conn = ds.getConnection();
I wrote a blog about that: http://angocadb2.blogspot.fr/2012/12/nombre-de-la-conexion-java-en-db2-java.html (Use your favorite translator because it is in Spanish)
According to this page on Info Center, there should be a function on the DB2Connection interface that allows you to change your application identifier, setDB2ClientApplicationInformation (I can't link directly, because there is no anchor, just search for that name).
You can pull the current application ID using the CURRENT CLIENT_APPLNAME special register:
SELECT CURRENT CLIENT_APPLNAME FROM SYSIBM.SYSDUMMY1
There are some other ways to set that register listed on the Info Center link listed above, including the WLM_SET_CLIENT_INFO function.
I am no DB2 expert, but I am looking at a trace record, generated by DB2 for z/OS, that contains a "correlation ID" (field QWHCCV in the product section correlation header of the trace record) that matches the value I set using setClientProgramName (method of the DB2 data source in my Java application).
My Java application is similar to the "DataSource" example given by AngocA, which is similar to the code quoted in the IBM technote 'The name of a DB2 JDBC application appears as "db2jcc_application". How to change it?'. This Java application, running on my Windows PC, connects to DB2 for z/OS. It also - and this is important, depending on which DB2 traces you have started (discussed below) - actually does something after connecting. For example:
pstmt=conn.prepareStatement("SELECT ... ");
rset=pstmt.executeQuery();
When you say, regarding the first example given by AngocA, "it doesn't do anything": what did you hope to see? Exactly where are you looking, what are you looking for, and what method (or tool) are you using to look for it?
For example, if you are looking for SMF type 100, 101, or 102 records (generated by DB2 traces) containing QWHCCV field values that match your correlation ID, then (with apologies if this is the bleeding obvious, teaching you how to suck eggs), on DB2 for z/OS, you need to start the DB2 traces (using the DB2 command START TRACE) that generate those records. Otherwise, there will be nothing to see ("it doesn't do anything"). Note that not all DB2 trace records generated by an application (such as the Java application described above) will contain your correlation ID; prior to a certain point in processing, the correlation ID of such records will have a different value (but that is getting off-topic, and anyway is about as far as I am comfortable describing).
Warning: Experiment with starting DB2 traces on a "sandbox" (development or test) DB2 system, not a production DB2 system. DB2 traces can result in large volumes of data.
You will also see the correlation ID in the message text of some DB2 V10 messages (such as DSNL027I) after "THREAD-INFO=".
For me I had to add a semicolon after each connection parameter.
EX for your case:
jdbc:db2://localhost:5036/DBNAME:clientProgramName=myprog;
EX with multiple params:
jdbc:db2://localhost:5036/DBNAME:clientProgramName=myprog;enableSysplexWLB=true;blah=true;

Password Encryption between Java Swing app and Postgres db

I'm building a system that has a Java Swing front end accessed a postgres database. Prior to discovering Jasypt this week, I had originally planned to use Postgres' own encryption mechanism. It worked fine, but I also now wanted the passwords over the network to be encrypted, hence why I turned to Jasypt.
Problem is, I need a fixed password to be entered into my Postgres stored function. I.e. If the input password is 'aaa' then any other inputed password into the Postgres stored function (other than 'aaa') will not match.
Is there a way to get these two encryption mechanisms to work in tandem together or do I have to dump Postgres'?
My user table:
CREATE TABLE "user"
(
id serial NOT NULL,
cryptpwd text NOT NULL,
md5pwd text NOT NULL,
...
)
Encrypting password:
cryptedPassword = crypt(passwordIn, gen_salt('md5'));
md5Password = md5(passwordIn);
INSERT INTO "user"(username, cryptpwd, md5pwd, ...)
VALUES (usernameIn, cryptedPassword, md5Password, ...);
Decrypting password:
select ..... from "user" .... where username = usernameIn and cryptpwd = crypt(passwordIn, cryptpwd);
If I cannot get the two of them to work together then I would have to dump Postgres' mechanism as I need to have encryption over the network.
Also, with regards to the database connection string and database username and password (not using any framework ... plain old jdbc connection hopefully with SSL - yet to implement), I don't think I'll be able to use Jasypt because I'd need to decrypt it at database level. Would SSL alone be sufficient for this case?
Thanks.
I think SSL alone, on every piece of the path, would be sufficient. In LedgerSMB (although we are Perl-based) we do something different and rely on SSL protected links between servers and between servers and clients. There are a few things to think about with your approach though.
We actually pass the db username and password to the middleware from the client in re-usable format (plain text) over an SSL connection, and then use another SSL connection to log into PostgreSQL to authenticate this way. This works fine, but the problem areas we face are somewhat similar to the problem areas you will. These include:
Logging. Is it possible passwords will get accidently logged? This is a concern with LedgerSMB and we take what steps we can but a badly configured server or a tampered-with program could log usernames and passwords. In our case this comes primarily on the middleware level, but in your case, query logging could do this too, right?
Is it possible credentials can be re-used unintentionally? We prevent this in a couple of ways, but it is worth considering.
On the whole, we trust SSL. There isn't much to be gained from adding additional encryption beyond that, and key management adds a lot of complexity that is not worth the marginal gains IMO.