Free Developer Support Forum

Guest Guest 

Show/Hide Header

Welcome Guest, posting in this forum requires registration.





Pages: [1]
Author Topic: how to load DB2 Type 2 JDBC driver
Kwun Ho
Administrator
Posts: 4
Post how to load DB2 Type 2 JDBC driver
on: July 28, 2010, 19:58

import java.sql.*;
import java.lang.*;
import java.io.*;
import COM.ibm.db2.jdbc.app.*; //DB2 UDB JDBC classes. Do not need this if
//are using type 4 driver
public class tgsample {
static
{
try
{
//Loads Type 2 Driver
Class.forName(“COM.ibm.db2.jdbc.app.DB2Driver”);

//For type 4 Driver: Class.forName(“com.ibm.db2.jcc.DB2Driver”);
}
catch (ClassNotFoundException e)
{
System.err.println(“Could not load DB2 driver \n”);
System.err.println(e.getMessage());
System.exit(1);
}
}

public static void main(String args[])
{

/* Type 4 driver url */
//String url = “jdbc:db2j:net://machine-name:port-number/TGSAMPLE”;

/* Type 2 driver url */
String url = “jdbc:db2:tgsample”;
String uname = null, psswrd = null;

/* Our query from the tutorial */
String query = “SELECT lastname,salary FROM viswanat.employee WHERE bonus = 500″;

/* We need username and password before we can connect */
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter your username on DataStar: “);
uname = br.readLine();

PasswordField pf = new PasswordField();
psswrd = pf.getPassword(“Enter your password: “);

} catch (IOException e) {
System.out.print(“Failed to get uname/passwd”);
System.out.println(“:” + e.getMessage());
System.exit(1);
}

try
{
//Connect to a database
Connection conn = DriverManager.getConnection(url, uname, psswrd);

//Create statement for the connections
Statement stmt = conn.createStatement();

//Execute the query
ResultSet rs = stmt.executeQuery(query);

//Loop through the result and print it out
System.out.println(query);
System.out.println(“\nLASTNAME \tSALARY”);
System.out.println(“—————————-”);

while (rs.next())
{
System.out.print(rs.getString(1) + ” \t” );
System.out.println(rs.getString(2));
}

rs.close();
stmt.close();
conn.close();
}
catch (SQLException e)
{
System.out.println(“SQL Exception: “);
System.err.println(e.getMessage());
}
}//main
} //class

/**
* This class prompts the user for a password and attempts to mask input with “”
* This code is taken from: http://java.sun.com/features/2002/09/pword_mask.html
* Note: you do not need this class in order to connect to a database;
* this is here only for your convenience.
*
*/

class PasswordField {

/**
*@param prompt The prompt to display to the user.
*@return The password as entered by the user.
*/

String getPassword(String prompt) throws IOException {
// password holder
String password = “”;
int counter = 0;
MaskingThread maskingthread = new MaskingThread(prompt);
Thread thread = new Thread(maskingthread);
thread.start();
// block until enter is pressed
while (true) {
char c = (char)System.in.read();
// assume enter pressed, stop masking
System.out.flush();
maskingthread.stopMasking();

if (c == ‘\r’) {
c = (char)System.in.read();
if (c == ‘\n’) {
break;
} else {
continue;
}
} else if (c == ‘\n’) {
break;
} else {
// store the password
password += c;
}
}
return password;
}
}

/**
* This class attempts to erase characters echoed to the console. It’s made to
* work with PasswordField class (see above).
*/

class MaskingThread extends Thread {
private boolean stop = false;
private int index;
private String prompt;

/**
*@param prompt The prompt displayed to the user
*/
public MaskingThread(String prompt) {
this.prompt = prompt;
}

/**
* Begin masking until asked to stop.
*/
public void run() {
while(!stop) {
try {
// attempt masking at this rate
this.sleep(1);
}catch (InterruptedException iex) {
iex.printStackTrace();
}
if (!stop) {
System.out.print(“\r” + prompt + ” \r” + prompt );
}
System.out.flush();
}
}

/**
* Instruct the thread to stop masking.
*/
public void stopMasking() {
this.stop = true;
}
}

AngelinaLOPEZ1-
9
Newbie
Posts: 1
Post Re: how to load DB2 Type 2 JDBC driver
on: August 17, 2010, 16:54

I had a desire to make my own organization, nevertheless I didn’t earn enough amount of cash to do that. Thank heaven my fellow told to take the <a href=”http://bestfinance-blog.com”>loans</a>. Thus I used the credit loan and made real my old dream.

Pages: [1]
Mingle Forum by cartpauj
Version: 1.0.11 ; Page loaded in: 0.025 seconds.

Comments are closed.