如何使用JSP串連DB2資料庫_JSP編程

來源:互聯網
上載者:User
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contentType="text/html; charset=gb2312"
%>
<html>
<head>
<title>使用Db2資料庫</title>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>
<body>
<p align="center"><u><font size="5" face="華文新魏">使用JSP串連到Db2資料庫</font></u></p>
<%!
String host="192.1.1.127"; //資料庫主機
String database="myweb"; //資料庫名
String user="db2admin"; //使用者名稱
String pass="db2admin"; //口令
%><% java.sql.Connection sqlConn; //資料庫連接對象
java.sql.Statement sqlStmt; //語句對象
java.sql.ResultSet sqlRst; //結果集對象
try{

try{
//登記JDBC驅動對象
//下面的二中方法均可
//DriverManager.registerDriver (new COM.ibm.db2.jdbc.net.DB2Driver()); //db2
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance(); //db2
}
catch (ClassNotFoundException e)
{ out.print (e);
}
//串連資料庫
sqlConn= java.sql.DriverManager.getConnection ("jdbc:db2://"+host+"/"+database,user,pass);//建立語句對象
out.print ("串連資料庫成功!");
%>
</body>
<%
//關閉結果集對象
  //sqlRst.close();
  //關閉語句對象
//sqlStmt.close ();
//關閉資料庫連接
sqlConn.close();
}catch (SQLException e)
{  out.print ("串連資料庫失敗!");
out.print (e);
}
%>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.