Application and development based on Weblogic 7.0

Source: Internet
Author: User
Tags sql server driver sql error stmt trim mysql database
Web 1. Database connection
This article takes a SQL Server database as an example of how to access a database through ConnectionPool and datasource.

1. Install the driver provided by Microsoft
Install JDBC for SQL Server driver, http://www.microsoft.com/downloads/details.aspx from Microsoft's website? familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en Download the JDBC driver, click Setup.exe, and install the driver. Modify the Classpath of WebLogic 7 and join
%sqlserver_jdbc%\lib\msbase.jar;
%sqlserver_jdbc%\lib\mssqlserver.jar;
%sqlserver_jdbc%\lib\msutil.jar
, restart WebLogic

2. Configure WebLogic.
Specific steps:
Joint use of ConnectionPool and DataSource, steps:
1) Establish a connection pool (ConnectionPool).
A. Select from the Console menu, services/jdbc/connection pools
B. Click Configure a new JDBC Connection Pool ...
C. Fill in the Configuration/general page sign (here according to different JDBC driver fill in different parameters)
Method One: Use Microsoft's JDBC for SQL Server driver:
Name:sqlserverpool
Url:jdbc:jdbc:microsoft:sqlserver://localhost:1433;databasename=master
Driver Classname:com.microsoft.jdbc.sqlserver.SQLServerDriver
Properties (key=value): User=sa
Password=sa


Method One: Use Bea's JDBC for SQL Server driver (there is a Chinese problem)
Name:sqlserverpool
Url:jdbc:jdbc:microsoft:sqlserver://localhost:1433;databasename=master
Driver Classname:com.microsoft.jdbc.sqlserver.SQLServerDriver
Properties (key=value): User=sa
Password=sa

D. Click Create
E.configuration/connections Tab:
Initial capacity:1
Maximum Capacity:5
The rest of the default
F. Click Apply
G.targets/servers Tab:
Select MyServer, click apply! If, no error, that is to prove the creation of connection pool success!

2 The connection pool is mapped to a data source (DataSource).
A. Select from the Console menu, Services/jdbc/data Sources
B. Click Configure a new JDBC Data Source ...
C.configuration Tab:
Name:sqlserverdatasource
JNDI Name:sqlserver
Pool Name:sqlserverpool
D. Click Create
E.targets/services Tab:
Select MyServer, click apply! If, no error, that is to prove the creation of data source success!

3. Writing test procedures
(The problem with Jndi is actually very simple, just a few steps, remember ok!):

Import java.sql.*;
Import java.util.*;

Import javax.naming.*;
Import javax.sql.*;
public class Datasourcetest
{
public static void Main (string[] args)
{
Statement stmt = null;
Connection conn = null;
ResultSet res = null;
try{
Hashtable env = new Hashtable ();
Env.put (Initialcontext.initial_context_factory, "weblogic.jndi.WLInitialContextFactory");
Env.put (Initialcontext.provider_url, "t3://localhost:7001"); WebLogic Port IP
Env.put (Initialcontext.security_principal, "system"); WebLogic Connect Users
Env.put (Initialcontext.security_credentials, "Sysmanager");//weblogic password
InitialContext CTX = new InitialContext (env);
DataSource ds = (DataSource) ctx.lookup ("SQL Server"); Jndi Name
conn = Ds.getconnection ();
stmt = Conn.createstatement ();
res = stmt.executequery ("SELECT * from TestTable");
SYSTEM.OUT.PRINTLN ("ID------name------address");
while (Res.next ()) {
int id = res.getint (1);
String name = res.getstring (2). Trim ();
String address = res.getstring (3). Trim ();
SYSTEM.OUT.PRINTLN (id+ "------" +name+ "------" +address);
}
}
catch (SQLException SSE) {
SYSTEM.OUT.PRINTLN ("SQL error!");
}
catch (Namingexception e) {
System.out.println ("Namingexception");
}
try{
Stmt.close ();
Conn.close ();
}
catch (SQLException se) {}
}
}


Report:
Settings for connecting to Oracle databases
Copy the Classes12.zip under the Oracle installation directory and add the package as a driver in WebLogic's classpath.
Fill in the following parameters in the Configuration/general tab of the ConnectionPool in WebLogic
Name:oraclerpool
Url:jdbc:oracle:thin:@[oracle Server ip]:1521:[Service name]
Driver Classname:oracle.jdbc.driver.OracleDriver
Properties (key=value): User=[oracle users]
Password=[oracle User Password]
Dll=ocijdbc8
Protocol=thin
Other settings you can set refer to the settings in SQL Server above.

Settings for connecting to MySQL database
Slightly

Note:
Defects with MS JDBC for SQL Server and BEA JDBC for SQL Server (not written)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.