Kbase P121595: How to connect a Progress Database using a Java Program
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  05/08/2008 |
|
Status: Verified
GOAL:
How to connect a Progress Database using a Java Program
GOAL:
Sample program to connect Progress Database from Java
GOAL:
How to setup a JDBC connection to Progress Database
FACT(s) (Environment):
OpenEdge 10.x
Windows
UNIX
FIX:
Set the following environment variables:
set DLC = <path for progress install dir>
set CLASSPATH=%CLASSPATH%;<path for jdbcjar> ie:C:\progress\100b\OpenEdge\java\jdbc.jar
set PATH=%PATH%;%DLC%\bin;%DLC%;
These environment variables are mandatory until 10.0B and below, because Progress Driver was not a pure java driver (see solution 20600, "What are the JDBC driver types and what types does Progress provide?"), depend on client networking dlls.
Starting on OpenEdge 10.1A these are no longer required.
Once this is done, run the sample code bellow. By default, it connects to a sports database running a SQL Broker on port 3500 (see solution P7843, "How to configure a secondary SQL-92 Broker for Progress 9.1D and above"), but connection URL and query will be prompted to allow different database and query.
This program just shows the first collumn of the query: import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class SelectCustomer {
protected static final PrintStream o = System.out;
public static void main(String args[]) {
try {
//ProgressVersion prg;
// supposes dadatabase is on 3500 and named sports
String url = "jdbc:JdbcProgress:T:localhost:3500:sports";
System.out.println("Defaultl url:" + url);
byte urlBytes[] = new byte[50];
System.out.print("Input a new URL or enter to accept this as default:");
System.in.read(urlBytes);
if (urlBytes[0]!=13) {
System.out.println("Using URL:" + new String(urlBytes).trim());
url = new String(urlBytes).trim();
}
String userid = "";
String passwd = "";
byte[] passwdBytes = new byte[15];
System.out.print("User:");
byte[] useridBytes = new byte[15];
System.in.read(useridBytes );
System.out.print("Password:");
System.in.read(passwdBytes );
passwd = new String(passwdBytes).trim();
userid = new String(useridBytes).trim();
// Load the driver
Class.forName("com.progress.sql.jdbc.JdbcProgressDriver");
// Attempt to connect to a driver. Each one
// of the registered drivers will be loaded until
// one is found that can process this URL
java.util.Properties prop = new java.util.Properties();
prop.put("user", userid);
prop.put("password", passwd);
Connection con = DriverManager.getConnection(url, prop);
DatabaseMetaData dma = con.getMetaData();
o.println("\nConnected to " + dma.getURL());
o.println("Driver " + dma.getDriverName());
o.println("Version " + dma.getDriverVersion());
String query = "select * from pub.customer";
System.out.println("query to be executed:" + query);
System.out.println("input a new query string or <enter> for default");
byte[] queryBytes = new byte[80];
System.in.read(queryBytes);
if (queryBytes[0]!= 13) {
query = new String(queryBytes).trim();
System.out.println("URL Used:" + query );
}
ResultSet rs = con.createStatement().executeQuery(query);
while (rs.next()) {
String name = rs.getString(1);
System.out.println(name);
}
//con.createStatement().executeUpdate("insert into customer ('name') values('miro')");
}
>
catch (java.lang.Exception ex) {
// Got some other type of exception. Dump it.
ex.printStackTrace();
}
}
} .