com.mysql.jdbc.PacketTooBigException java - java

I'm having this problem when trying to connect to my database. Change the value of the variable but still this error persists
Value mysql
##max_allowed_packet
16777216
Java Code
Properties connProps = new Properties();
connProps.put("user", Config.DB_USER);
connProps.put("password", Config.DB_PASS);
this._conn = DriverManager.getConnection("jdbc:" + Config.DB_DBMS + "://" + Config.DB_HOST + ":"
+ Config.DB_PORT + "/" + Config.DB_NAME, connProps);
Error :
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
Modify the my.cnf file on my server

The jdbc client, also has a "maxAllowedPacket" setting.
You may set your jdbc url like:
jdbc:mysql://192.168.15.1/dbname?useUnicode=true&characterEncoding=UTF-8&maxAllowedPacket=16777216

Had the same error, trying to connect to a MySQL database using DataGrip 2016.1 (from JetBrains).
Troubleshooting further, I've realised that my records entry were wrong; the database password was not correct.
Upon rectification using the correct database password, I no longer encountered the error and was able to connect successfully.

Related

Connect to an external Oracle DB using TNS

I need to connect to an external database to copy data from there to my table. I have a TNS file for this external database, and I am trying to connect using JDBC like this:
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
host +
")(PORT=" +
port +
")))(CONNECT_DATA=(SERVICE_NAME=" +
service +
")))",
user,
password);
...
But when trying to connect, i get the error java.net.UnknownHostException (host is not recognized). I guess the problem is that this is an internal host and I don't have access to it.
How to connect to the database using TNS?
You should not need the full TNS text. The following should suffice
getConnection("jdbc:oracle:thin:myuser/mypass#//"+host+":"+port+"/"+service);
If you have a tnsnames.ora then, you can provide the TNS alias as part of your connection string. Make sure you try to login to the Oracle Database through sqlplus using the connection string present in tnsnames.ora.
// dbname_tnsalias - It is the TNS alias present in tnsnames.ora.
// TNS_ADMIN --> Absolute path where tnsnames.ora is present.
final String DB_URL="jdbc:oracle:thin:#dbname_tnsalias?TNS_ADMIN=/Users/test/";

Directory does not exists (Lotus Notes)

I'm trying to get all the database on my server. But specifying my server name in getDbDirectory() parameter as NALLN304/40/LLN/IBM gives me a error.
Directory NALLN304/40/LLN/IBM!! does not exist
it always add two exclamation mark at the end. I tried also as server name and mail file adding .nsf format at the end of mail file. NALLN304/40/LLN/IBM!!data0\126\1000031540.nsf also gives me the same error.
Snipper code below:
Session session = null;
Database db = null;
DbDirectory dir = null;
try
{
NotesThread.sinitThread();
session = NotesFactory.createSession();
System.out.println("User = " + session.getUserName());
dir = session.getDbDirectory("NALLN304/40/LLN/IBM");
System.out.println(dir.getName());
db = dir.getFirstDatabase(DbDirectory.DATABASE);
do
{
System.out.println("Title: " +db.getTitle());
}
while(dir.getNextDatabase() != null);
}
catch(NotesException ex)
{
ex.printStackTrace();
}
The error always points out to the db = dir.getFirstDatabase(DbDirectory.DATABASE); because dir.getFirstDatabase(DbDirectory.DATABASE) expects .nsf file even I specify the file format. Any reasons why I got this error?
Your problem is that your ID is not authenticating with the server. You need to take the output from this line:
System.out.println("User = " + session.getUserName());
And take it to your server administrators, ask why it is not being allowed to access the server, and ask them to assist you either by granting the necessary permissions or by giving you another ID that you can use.
Check the logs for server connection errors. Even if the server has to connect to itself. Faced the same problem. The server gave an error because it did not find a route for the connection. Added a new connection for the server to itself and everything was fixed

2 databases, one I can use over the LAN and one must be localhost, why?

I have 2 database: harrington, which I created myself and pacsdb which I inherited. Both of these 2 are located in the same MySQL database. I am using Netbeans to debug things. In Netbeans I can connect to the database by
jdbc:mysql://192.168.0.100:3306/harrington?zeroDateTimeBehavior=convertToNull [fiji on Default schema]
jdbc:mysql://192.168.0.100:3306/pacsdb?zeroDateTimeBehavior=convertToNull [pacs on Default schema]
jdbc:mysql://localhost:3306/pacsdb?zeroDateTimeBehavior=convertToNull [pacs on Default schema]
To my own database, harrington, I have no problems either in Netbeans or in my own Java program. Pacsdb will not connect using 192.168.0.100, but it will connect using localhost. In my program it gives
java.sql.SQLException: Access denied for user 'pacs'#'192.168.0.100' (using password: YES)
To check what is going on I queried the sql database table 'user'. I got that both users use '%' as Host which is what I would expect from the code
sql = "create user 'pacs'#'%' identified by 'pacs'";
OK1 = executeStatement(stm1, sql);
if(OK1) {
sql = "grant all on pacsdb.* to 'pacs'#'%' identified ";
sql += "by 'pacs1' with grant option";
executeStatement(stm1, sql);
sql = "flush privileges";
executeStatement(stm1, sql);
}
I did find a difference when I queried the sql 'db' table. There I got 2 entries
Host Db User
% harrington fiji
localhost pacsdb pacs
I tried to delete the entry for pacsdb with localhost and insert a new entry for pacsdb with Host set to '%'. Apparently that isn't way to change from localhost, because I had difficulty to reach pacsdb. I deleted the entry from 'db' and inserted a new entry with '%' set back to 'localhost'. That got me back on line with the access denied error.
So my question is: how do I change from 'localhost' to '%'?
The problem is not the query, but the connection to the database.
MySql has the possibility to limit the access from users accessing it from an IP different from localhost.
Probably you have not the right to access the database from your ip.
Try to add the correct right with the following command
GRANT ALL PRIVILEGES
ON database.*
TO 'user'#'*'
IDENTIFIED BY 'newpassword';
Obviously you can't connect remotely to do that command. You need to enter via SSH on the remote machine and launch that command locally.

how to upload data into oracle from csv file where client and server are on different physical systems using java

I want to upload data from csv file on client to oracle database on server which is physically on different machine using java. I searched online for solutions but all solutions are working only for the case where both client and server are on same machine.
Please help
String temp = ((System.getenv("USERPROFILE"))+("\\Documents\\"));
String path = "";
path = temp.replace("\\", "\\\\");
path = path + "upload.csv";
//System.out.println(path);
Connection connection = MyConnection.getConnection();
String loadQuery = "LOAD DATA LOCAL INFILE '" + path + "' INTO TABLE documents FIELDS TERMINATED BY ','" + " LINES TERMINATED BY '\n' ( Customer, address1 )";
//System.out.println(loadQuery);
Statement stmt = connection.createStatement();
stmt.execute(loadQuery);
This path works if the server and client are on same machine, how to modify this when server and client are on different machine ?
If I understand well you have
A server with oracle
A server with java
A file csv on the server with java
And you need to save the data from csv to oracle. Correct?
If so this is a standard java application with I/O to read the file and JDBC access to a remote database.
Basically here are the steps:
1) Open the csv
2) Open a database connection
3) Read a row from csv and convert it to an object (if using JPA)
4) Save the object
5) Go to the next row until finish
6) Commit and close all resources
To access a remote database simply substitute 127.0.0.1 (or localhost) from the url connection with the ip of the remote database.

Getting the Server Name from the session

In a managed bean that resides in a Database on the server Development I have this code:
s = ExtLibUtil.getCurrentSession();
theMap.put("Server Name", s.getServerName());
when I look at theMap after this has run I see Server Name and the value is blank. After this I get a datbase RepID and then try to open the database by RepID with
appDB = s.getDbDirectory(null).openDatabaseByReplicaID(repID);
if (appDB.isOpen()){
theMap.put(thisKey, repID);
}else{
theMap.put("DB " + thisKey, "Is Not Open");
}
if I have a rep copy of the database locally it opens it, if I remove the local Replica the open fails. If I change the line to:
appDB = s.getDbDirectory("Development").openDatabaseByReplicaID(repID);
the proper appDB opens. So it looks like the session thinks it is running locally because it return null for the server name. This is really strange, am I missing something? For the moment i have just hard coded the server name in the getDbDirectory but that wont work in the real world.
Is this XPiNC? That would consider the database to be running locally unless you've set the application property "Run server-based XPages on server"
String serverName = s.getEnvironmentString("ServerName", true);
or
String serverName = s.getEnvironmentString("ServerKeyFileName_Owner", true);

Categories