国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp 連接數據庫大全

2024-09-05 00:19:28
字體:
來源:轉載
供稿:網友

許多初學jsp的網友經常會問數據庫怎么連接啊,我總結一下,對一些資源進行了搜集和整理,僅供大家參考,其實這種把數據庫邏輯全部放在jsp里未必是好的做法,但是有利于初學者學習,當大家學到一定程度的時候,可以考慮用mvc的模式開發。在練習這些代碼的時候,你一定將jdbc的驅動程序放到服務器的類路徑里,然后要在數據庫里建一個表test,有兩個字段比如為test1,test2,可以用下面sql建:

  create table test(test1 varchar(20),test2 varchar(20))

   然后向這個表寫入一條測試紀錄,那么現在開始我們的jsp和數據庫之旅吧。

一、jsp 連接oracle8/8i/9i 數據庫(用thin 模式)
  testoracle.jsp 如下:

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("oracle.jdbc.driver.oracledriver").newinstance();

string url="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl為你的數據庫的sid

string user="scott";

string password="tiger";

connection conn= drivermanager.getconnection(url,user,password);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);

string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>

您的第一個字段內容為:<%=rs.getstring(1)%>

您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>

<%out.print("數據庫操作成功,恭喜你");%>

<%
rs.close();
stmt.close();
conn.close();
%>

</body>
</html>
 

[separator]

二、jsp 連接sql server數據庫
  testsqlserver.jsp 如下: <%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver").newinstance();

string url="jdbc:microsoft:sqlserver: //localhost:1433;databasename=pubs";
//pubs為你的數據庫的

string user="sa";

string password="";

connection conn= drivermanager.getconnection(url,user,password);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>

您的第一個字段內容為:<%=rs.getstring(1)%>

您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>

<%out.print("數據庫操作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>
</html>
三、jsp 連接db2 數據庫

  testdb2.jsp 如下:

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("com.ibm.db2.jdbc.app.db2driver ").newinstance();

string url="jdbc:db2: //localhost:5000/sample";

//sample為你的數據庫名

string user="admin";

string password="";

connection conn= drivermanager.getconnection(url,user,password);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>

您的第一個字段內容為:<%=rs.getstring(1)%>

您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>


<%out.print("數據庫操作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>


</body>
</html>

四、jsp 連接informix 數據庫
  testinformix.jsp 如下:

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("com.informix.jdbc.ifxdriver").newinstance();

string url = "jdbc:informix-sqli: //123.45.67.89:1533/testdb:informixserver=myserver;

user=testuser;password=testpassword";

//testdb為你的數據庫名

connection conn= drivermanager.getconnection(url);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>

您的第一個字段內容為:<%=rs.getstring(1)%>

您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>

<%out.print("數據庫操作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>
</html>
五、jsp 連接sybase 數據庫
  testmysql.jsp 如下:

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("com.sybase.jdbc.sybdriver").newinstance();

string url =" jdbc:sybase:tds:localhost:5007/tsdata";

//tsdata為你的數據庫名

properties sysprops = system.getproperties();

sysprops.put("user","userid");

sysprops.put("password","user_password");

connection conn= drivermanager.getconnection(url, sysprops);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>

您的第一個字段內容為:<%=rs.getstring(1)%>


您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>

<%out.print("數據庫操作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>
</html>

六、jsp 連接mysql 數據庫
  testmysql.jsp 如下:

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("org.gjt.mm.mysql.driver").newinstance();

string url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useunicode=true&characterencoding=8859_1"
//testdb為你的數據庫名

connection conn= drivermanager.getconnection(url);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>


您的第一個字段內容為:<%=rs.getstring(1)%>

您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>

<%out.print("數據庫操作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>
</html>

七、jsp 連接postgresql 數據庫
  testmysql.jsp 如下:

<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<body>

<%class.forname("org.postgresql.driver").newinstance();

string url ="jdbc:postgresql://localhost/soft"

//soft為你的數據庫名

string user="myuser";

string password="mypassword";

connection conn= drivermanager.getconnection(url,user,password);

statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);

string sql="select * from test";

resultset rs=stmt.executequery(sql);

while(rs.next()) {%>

您的第一個字段內容為:<%=rs.getstring(1)%>

您的第二個字段內容為:<%=rs.getstring(2)%>

<%}%>

<%out.print("數據庫操作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>
</html>

 

八、jsp 連接access 數據庫 

<%@page import="java.sql.*"

import ="java.util.*"

import ="java.io.*"

import="java.text.*"

contenttype="text/html; charset=gb2312"

buffer="20kb"

%><%! int all,i,m_count;

string odbcquery;

connection odbcconn;

statement odbcstmt;

resultset odbcrs;

string username,title,content,work,email,url,time,date;

string datetime;

%>

<%

try{

class.forname("sun.jdbc.odbc.jdbcodbcdriver");

}catch (classnotfoundexception e)

{ out.print ("驅動程序不存在");

}

try{

odbcconn = drivermanager.getconnection("jdbc:odbc:db1");


odbcstmt = odbcconn.createstatement();


odbcquery="select * from book where datetime>2001-4-26 order by datetime desc";

odbcrs=odbcstmt.executequery(odbcquery);

int i=0;

while (i<130) odbcrs.next();

while (odbcrs.next())

{

//*/////////////////////////顯示數據庫的內容用于調試程序是用//
int ii;

try{

try{

for (ii=1;;ii++)

out.print ("<br>cloumn "+ii+" is: "+odbcrs.getstring(ii));

}catch (nullpointerexception e) {

out.print ("有空的指針");

}

}catch (sqlexception e){

}

}

odbcrs.close();

odbcstmt.close();

odbcconn.close();

}catch (sqlexception e)

{ out.print (e);

}

%>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 犍为县| 大余县| 池州市| 内黄县| 自贡市| 嘉祥县| 道真| 西乡县| 安龙县| 德庆县| 苏尼特左旗| 襄汾县| 广饶县| 定陶县| 道真| 若尔盖县| 和政县| 深州市| 和顺县| 油尖旺区| 汨罗市| 化隆| 茌平县| 凌源市| 三原县| 宿州市| 萍乡市| 马尔康县| 石泉县| 昌邑市| 巴彦淖尔市| 崇阳县| 马龙县| 玛多县| 古蔺县| 安溪县| 治多县| 广宗县| 赤峰市| 和林格尔县| 永州市|