您好,登錄后才能下訂單哦!
這篇“JSP如何連接SQL數(shù)據(jù)庫”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“JSP如何連接SQL數(shù)據(jù)庫”文章吧。
(1)導(dǎo)入sql包
<%@ page import = "java.sql.*" %>
(2)加載數(shù)據(jù)庫驅(qū)動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
(3)建立數(shù)據(jù)庫連接
Connection ct=DriverManager.getConnection( url,user,password);
這里的url,user,password 。 url 指定數(shù)據(jù)庫,user 是你數(shù)據(jù)庫成員的用戶名,password 是密碼,一會看下面的完整代碼就明白了。
(4)發(fā)送sql語句(兩種方法)
方法一:statement
執(zhí)行查詢功能(select)
Statement stmt = ct.createStatement(); String sql = "select * from 賬號"; // 執(zhí)行數(shù)據(jù)庫查詢語句 rs = stmt.executeQuery(sql);
執(zhí)行更新增刪改操作(insert,delete,update)
Statement stmt = ct.createStatement(); String sql = "update 學(xué)生表 set 學(xué)號='201601000' where 姓名 = '張三'"; stmt.executeUpdate(sql);
方法二:preparedstatement
String sql = "update 學(xué)生表 set 學(xué)號=? where 姓名 = ?"; st.executeUpdate(sql); PreparedStatement ps = ct.prepareStatement(sql); ps.setString(1, "201601000"); ps.setString(2, "張三"); ps.executeUpdate();
(5)如果查詢的話處理結(jié)果集(ResultSet)
查看完整代碼:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% PreparedStatement ps = null; Connection ct = null; ResultSet rs = null; String url = "jdbc:sqlserver://localhost:1433;databaseName=test"; String user="sa"; //超級管理員 String password="***********"; //密碼 try { //1.加載驅(qū)動 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("加載驅(qū)動成功!"); }catch(Exception e) { e.printStackTrace(); System.out.println("加載驅(qū)動失??!"); } try { //2.連接 ct=DriverManager.getConnection( url,user,password); System.out.println("連接數(shù)據(jù)庫成功!"); }catch(Exception e) { e.printStackTrace(); System.out.println("連接數(shù)據(jù)庫失??!"); } out.println("select * from emp"+"<br>"); /*嘗試查詢數(shù)據(jù)庫*/ try{ Statement stmt = ct.createStatement(); String sql = "select * from emp"; // 執(zhí)行數(shù)據(jù)庫查詢語句 rs = stmt.executeQuery(sql); while (rs.next()) { String id = rs.getString("eno"); String name = rs.getString("ename"); String age = rs.getString("sex"); out.println("eno:" + id +"\t"+ "ename:" + name +"\t"+"sex:" + age+"<br>"); } if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (ct != null) { ct.close(); ct = null; } } catch (SQLException e) { e.printStackTrace(); System.out.println("數(shù)據(jù)庫連接失敗"); } %> </body> </html>
以上就是關(guān)于“JSP如何連接SQL數(shù)據(jù)庫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。