Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

JSP and SQL Server Database (Java)

JSP and SQL Server Database (Java) หัวข้อนี้จะเป็นการเขียน JSP กับการติดต่อกับฐานข้อมูล Database ของ SQL Server แบบง่าย ๆ โดยจะยกตัวอย่างการเขียนเพื่อติดต่อกับ SQL Server เช่น การเชื่อมต่อว่ามีวิธีการอย่างไร การอ่านข้อมูลจาก SQL Server มาแสดงในหน้า Webpage และตัวอย่าง Code สำหรับการ Insert ข้อมูล , Update ข้อมูล และการ Delete ข้อมูล ซึ่งตัวอย่างนี้สามารถนำไปประยุกต์ใช้งานกับการเขียน JSP กับ Databae SQL Server ได้หลากหลาย

JSP and SQL Server Database

ไฟล์ jar ซึ่งเป็๋น Connector Library ไว้ติดต่อกับ Database ของ SQL Server

Table
CREATE TABLE [dbo].[customer](
	[CustomerID] [varchar](4) NOT NULL,
	[Name] [varchar](50) NULL,
	[Email] [varchar](50) NULL,
	[CountryCode] [varchar](2) NULL,
	[Budget] [float] NULL,
	[Used] [float] NULL,
 CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED 
(
	[CustomerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO customer VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO customer VALUES ('C002', 'John  Smith', '[email protected]', 'UK', 2000000, 800000);
INSERT INTO customer VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO customer VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);


JSP and SQL Server Database

โครงสร้างของ Database

รูปแบบการเชื่อมต่อระหว่าง JSP กับ SQL Server

		Connection connect = null;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
			connect =  DriverManager.getConnection("" +
					"jdbc:sqlserver://localhost\\SQL2008;databaseName=mydatabase;user=sa;password=");

			if(connect != null){
				out.println("Database Connected.");
			} else {
				out.println("Database Connect Failed.");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			connect.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}


Example ตัวอย่างการเขียน JSP เพื่ออานข้อมูลจาก SQL Server มาแสดงในหน้า Webpage

index.jsp
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<html>
<head>
	<title>ThaiCreate.Com JSP Tutorial</title>
</head>
<body>
	
	<%
	Connection connect = null;
	Statement s = null;
	
	try {
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		
		connect =  DriverManager.getConnection("" +
				"jdbc:sqlserver://localhost\\SQL2008;databaseName=mydatabase;user=sa;password=");
		
		s = connect.createStatement();
		
		String sql = "SELECT * FROM  customer ORDER BY CustomerID ASC";
		
		ResultSet rec = s.executeQuery(sql);
		%>
		<table width="600" border="1">
		  <tr>
		    <th width="91"> <div align="center">CustomerID </div></th>
		    <th width="98"> <div align="center">Name </div></th>
		    <th width="198"> <div align="center">Email </div></th>
		    <th width="97"> <div align="center">CountryCode </div></th>
		    <th width="59"> <div align="center">Budget </div></th>
		    <th width="71"> <div align="center">Used </div></th>
		  </tr>	
			<%while((rec!=null) && (rec.next())) { %>
				  <tr>
				    <td><div align="center"><%=rec.getString("CustomerID")%></div></td>
				    <td><%=rec.getString("Name")%></td>
				    <td><%=rec.getString("Email")%></td>
				    <td><div align="center"><%=rec.getString("CountryCode")%></div></td>
				    <td align="right"><%=rec.getFloat("Budget")%></td>
				    <td align="right"><%=rec.getFloat("Used")%></td>
				  </tr>
	       	<%}%>
	  	</table>      
	    <%	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
	
		try {
			if(s!=null){
				s.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
	%>
</body>
</html>

Output

JSP and SQL Server Database








ตัวอย่างรูปแบบการเชื่อมต่อและกระทำกับ Database เช่น Add Insert/Update/Delete

Insert
		Connection connect = null;
		Statement s = null;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
			connect =  DriverManager.getConnection("" +
					"jdbc:sqlserver://localhost\\SQL2008;databaseName=mydatabase;user=sa;password=");
			
			s = connect.createStatement();
			
			String sql = "INSERT INTO customer " +
					"(CustomerID,Name,Email,CountryCode,Budget,Used) " + 
					"VALUES ('C005','Chai Surachai','[email protected]'" +
					",'TH','1000000','0') ";
             s.execute(sql);
            
             out.println("Record Inserted Successfully");
             
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			if(s != null) {
				s.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}


Read
		Connection connect = null;
		Statement s = null;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
			connect =  DriverManager.getConnection("" +
					"jdbc:sqlserver://localhost\\SQL2008;databaseName=mydatabase;user=sa;password=");
			
			s = connect.createStatement();
			
			String sql = "SELECT * FROM  customer ORDER BY CustomerID ASC";
			
			ResultSet rec = s.executeQuery(sql);
			
			while((rec!=null) && (rec.next()))
            {
                out.print(rec.getString("CustomerID"));
                out.print(" - ");
                out.print(rec.getString("Name"));
                out.print(" - ");
                out.print(rec.getString("Email"));
                out.print(" - ");
                out.print(rec.getString("CountryCode"));
                out.print(" - ");
                out.print(rec.getFloat("Budget"));
                out.print(" - ");
                out.print(rec.getFloat("Used"));
                out.println("");
            }
             
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			if(s != null) {
				s.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}









Update
		Connection connect = null;
		Statement s = null;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
			connect =  DriverManager.getConnection("" +
					"jdbc:sqlserver://localhost\\SQL2008;databaseName=mydatabase;user=sa;password=");
			
			s = connect.createStatement();
			
			String sql = "UPDATE customer " +
					"SET Budget = '5000000' " +
					" WHERE CustomerID = 'C005' ";
             s.execute(sql);
            
             out.println("Record Update Successfully");
             
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			if(s != null) {
				s.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}


Delete
		Connection connect = null;
		Statement s = null;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
			connect =  DriverManager.getConnection("" +
					"jdbc:sqlserver://localhost\\SQL2008;databaseName=mydatabase;user=sa;password=");
			
			s = connect.createStatement();
			
			String sql = "DELETE FROM customer " +
					" WHERE CustomerID = 'C005' ";
             s.execute(sql);
            
             out.println("Record Delete Successfully");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			if(s != null) {
				s.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.println(e.getMessage());
			e.printStackTrace();
		}


สำหรับการใช้งาน JSP กับ SQL Server สามารถอ่านเพิ่มเติมได้ที่บทความ Java กับ SQL Server

JSP and SQL Server Database

Java and SQL Server Database (JDBC/SQLServerDriver)



.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-08-12 18:39:24 / 2017-03-27 15:59:22
  Download : Download  JSP and SQL Server Database (Java)
 Sponsored Links / Related

 
JSP and MS Access Database (Java)
Rating :

 
JSP and MySQL Database (Java)
Rating :

 
JSP and MariaDB Database (Java)
Rating :

 
JSP and Azure SQL Database (Windows Azure)
Rating :

 
JSP and Oracle Database (Java)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่