Get the App
SLTechnology News&Howtos  ›  Database  › 

How Java accesses SSL enabled DB2 Database

Shulou Source: shulou.com Published: 2022-05-31 13:54:52 09月27日 Update

This article is about how Java accesses SSL enabled DB2 Database. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Generate the JAVA JKS certificate file and prepare it for JDBC. The password here is the password used to encrypt the file. You can set it at will, and set it to hell0man here.

You don't have to generate a certificate file on DB2 Server, anywhere you can run Java.

The mydbserver.arm here is taken from DB2 Server.

$keytool-import-trustcacerts-file "mydbserver.arm"-keystore "mynewdbclient.jks" Sample Java code

=

Public class SSLTest

{

Public static void main (String [] args)

{

String ServerName = "192.168.110.10"

Int PortNumber = 50001

String DatabaseName = "TEST"

Java.util.Properties properties = new java.util.Properties ()

Properties.put ("user", "db2inst1");-- > user name for accessing the database

Properties.put ("password", "passw0rd");-- > password to access the database

Properties.put ("sslConnection", "true")

System.setProperty ("javax.net.ssl.trustStore", "C:\ temp\\ ssl_client\\ mynewdbclient.jks")

System.setProperty ("javax.net.ssl.trustStorePassword", "hell0man")

String url = "jdbc:db2://" + ServerName + ":" + PortNumber + "/" + DatabaseName+ ": traceFile=foobar.txt;traceLevel=" + 0xFFFFFFFFFF + ";"

Java.sql.Connection con = null

Try

{

Class.forName (com.ibm.db2.jcc.DB2Driver) .newInstance ()

}

Catch (Exception e)

{

System.out.println ("Error: failed to load Db2 jcc driver.")

}

Try

{

System.out.println ("url:" + url)

Con = java.sql.DriverManager.getConnection (url, properties)

Java.sql.Statement S2 = con.createStatement ()

Try

{

S2.executeUpdate ("drop table T1")

}

Catch (Exception e)

{

System.out.println ("drop is failing")

}

Try

{

S2.executeUpdate ("create table T1 (C1 int)")

}

Catch (Exception e)

{

System.out.println ("create is failing")

}

String str = "insert into T1 values (100)"

S2.executeUpdate (str)

Java.sql.PreparedStatement ps = con.prepareStatement ("select * from T1")

Java.sql.ResultSet rs = ps.executeQuery ()

While (rs.next ())

{

System.out.println (rs.getString (1))

}

Con.close ()

}

Catch (Exception e)

{

E.printStackTrace ()

}

}

}

Thank you for reading! This is the end of the article on "how Java accesses SSL enabled DB2 Database". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Passwords files content data databases more articles certificates good practical local articles users user names see knowledge birth preparation encryption reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL NVidia Microsoft OPPO Reno