jsp資料庫連接經典簡潔代碼,jsp操作資料庫

本文目錄一覽:

JSP連接資料庫的代碼

Class.forName(“com.mysql.jdbc.Driver”);

Connection conn=DriverManager.getConnection(“jdbc:mysql://”+host+”/”+dbname,username,password);

Statement Stmt=conn.createStatement();

ResultSet rs=Stmt.executeQuery(sql);

這是基本的資料庫操作方法

大概流程就是載入驅動類,創建連接,執行資料庫操作,關閉

jsp怎麼連接mysql資料庫代碼

jsp連接mysql資料庫的操作方式。

1、在數據服務端安裝好mysql資料庫,這個是必須的,在自己的ssh或者虛擬機上,數據mysql可以看到相關的提示,說明安裝成功

2、我是用的是tomcat伺服器,在這裡需要安裝好java連接mysql的資料庫操作庫。我是用的jar包是:mysql-connector-java-3.1.14.tar.gz,大家可以在網上下載,或者,在官網上下載

3、把解包後的jar放到tomcat裡面的lib文件夾下

4、在程序的代碼段里添加連接函數庫和庫函數,就可以連接到mysql資料庫了

5、剩下的就是我們使用的時候調用這樣的數據了,在jsp里使用mysql資料庫中的數據

急!懸賞200分,求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

二、jsp連接Sql Server7.0/2000資料庫

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=softpassword=soft1234useUnicode=truecharacterEncoding=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

0頂一下

求一個JSP連接資料庫的代碼

%@ page contentType=”text/html;charset=gb2312″ import=”java.sql.*”%

html xmlns=””

head

meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /

title登錄網站/title

/head

body

center

h1用戶登錄系統/h1

hr

br

br

%

// 接收用戶提交的內容

String ming = request.getParameter(“zhh”) ;

String mima = request.getParameter(“mm1”) ;

// 定義變數,如果用戶是合法用戶,則將此標記變為true

boolean flag = false ;

%

%

// 定義資料庫操作的常量、對象

// 資料庫驅動程序

String DBDRIVER = “com.microsoft.jdbc.sqlserver.SQLServerDriver” ;

// 資料庫連接字元串

String DBURL = “jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspdb” ;

// 資料庫用戶名

String DBUSER = “sa” ;

// 資料庫連接密碼

String DBPWD = “280271” ;

// 聲明一個資料庫連接對象

Connection conn = null ;

// 聲明一個資料庫操作對象

PreparedStatement pstmt = null ;

// 聲明一個結果集對象

ResultSet rs = null ;

// 聲明一個SQL變數,用於保存SQL語句

String sql = null ;

%

%

// 進行資料庫操作

try

{

// 編寫SQL語句

sql = “SELECT name FROM users WHERE name=? and password=?” ;

// 載入驅動程序

Class.forName(DBDRIVER) ;

// 連接資料庫

conn = DriverManager.getConnection(DBURL,DBUSER,DBPWD) ;

// 實例化資料庫操作對象

pstmt = conn.prepareStatement(sql) ;

// 設置pstmt的內容

pstmt.setString(1,ming) ;

pstmt.setString(2,mima) ;

// 查詢記錄

rs = pstmt.executeQuery() ;

// 判斷是否有記錄

if(rs.next())

{

// 如果有記錄,則執行此段代碼

// 用戶是合法的,可以登陸

flag = true ;

session.setAttribute(“name”,ming);

}

// 依次關閉

rs.close() ;

pstmt.close() ;

conn.close() ;

}

catch(Exception e)

{}

%

%

// 判斷用戶名及密碼

if(flag)

{

// 合法用戶

%

jsp:forward page=”zhuye.jsp”/

%

}

else

{

// 非法用戶

%

jsp:forward page=”denglushibai.jsp”/

%

}

%

/center

/body

/html

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/272040.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-17 00:08
下一篇 2024-12-17 00:08

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python棧操作用法介紹

    如果你是一位Python開發工程師,那麼你必須掌握Python中的棧操作。在Python中,棧是一個容器,提供後進先出(LIFO)的原則。這篇文章將通過多個方面詳細地闡述Pytho…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • Python操作數組

    本文將從多個方面詳細介紹如何使用Python操作5個數組成的列表。 一、數組的定義 數組是一種用於存儲相同類型數據的數據結構。Python中的數組是通過列表來實現的,列表中可以存放…

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29

發表回復

登錄後才能評論