jspsql
① Jsp中的sql語句寫法,高手進哦~~
String sql = "select * from record where choice like '"+%gjz%+"' and date >= '"+sdate+"' and date <='"+edate+"' order by date ";
ResultSet rs=stmt.executeQuery(sql);
'"+%gjz%+"'沒有錯
② jsp的sql語句的寫法
SELECT * FROM 表名 WHERE Index_img!=''
SELECT * FROM 表名 WHERE Index_img!='' AND User_Name='用戶名'
SELECT * FROM 表名 WHERE Index_img!='' AND News_Class='新聞分類'
SELECT * FROM 表名 WHERE User_Name='用戶名'
SELECT * FROM 表名 WHERE News_Class='新聞分類'
③ jsp連接sql資料庫,並用jsp把數據導入資料庫中
JSP連接SQL資料庫實現查找(支持模糊查找,查找年齡段),插入信息<實例>
<h2>學生信息查詢</h2>
<form method="POST" action="Name.jsp">
<h4>按姓名查找(支持模糊查詢)</h4>
<table bgcolor="#CCCCCC">
<tr>
<td>查找姓名</td>
<td><input type="text" name="name" size="15" /></td>
<td><input type="submit" value="查找"></td>
</tr>
</table>
</form>
<br/>
<form method="POST" action="Age.jsp">
<h4>按年齡查找</h4>
<table border="1" bgcolor="#CCCCCC">
<tr>
<td>查找年齡</td>
<td><input type="text" name="agemin" size="5" /></td>
<td>到</td>
<td><input type="text" name="agemax" size="5" /></td>
<td><input type="submit" value="查找"></td>
</tr>
</table>
</form>
<form action="Insert.jsp" method="POST">
<h4>插入信息到表中</h4>
<table border="1" bgcolor="#cccccc">
<tr>
<td>姓名</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>性別</td>
<td><input type="text" name="sex" /></td>
</tr>
<tr>
<td>年齡</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>系別</td>
<td><input type="text" name="dept" /></td>
</tr>
<tr>
<td><input type="submit" value="插入" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
④ 如何在jsp中執行多條sql語句
jsp同時執行多條sql,需要封裝成存儲過程,否則效率很低,甚至會引起性能問題。
jsp觸發後台java調用存儲過程的例子:
進行調用的詳細代碼:
try{
int age = 39;
String poetName = "dylan thomas";
CallableStatement proc = connection.prepareCall("{ call set_death_age(?, ?) }");
proc.setString(1, poetName);
proc.setInt(2, age);
cs.execute();
}catch (SQLException e){ // ....}
傳給prepareCall方法的字串是存儲過程調用的書寫規范。它指定了存儲過程的名稱,?代表了需要指定的參數。
⑤ 如何用JSP連接SQL資料庫..
你好,1.將資料庫驅動程序的JAR文件放在Tomcat的 common/lib 中;
2.在server.xml中設置數據源,以MySQL資料庫為例,如下:
在<GlobalNamingResources> </GlobalNamingResources>節點中加入,
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1:3306/test"
maxActive="4"/>
屬性說明:name,數據源名稱,通常取」jdbc/XXX」的格式;
type,」javax.sql.DataSource」;
password,資料庫用戶密碼;
driveClassName,資料庫驅動;
maxIdle,最大空閑數,資料庫連接的最大空閑時間。超過空閑時間,資料庫連
接將被標記為不可用,然後被釋放。設為0表示無限制。
MaxActive,連接池的最大資料庫連接數。設為0表示無限制。
maxWait ,最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示
無限制。
3.在你的web應用程序的web.xml中設置數據源參考,如下:
在<web-app></web-app>節點中加入,
<resource-ref>
<description>MySQL DB Connection Pool</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
子節點說明: description,描述信息;
res-ref-name,參考數據源名字,同上一步的屬性name;
res-type,資源類型,」javax.sql.DataSource」;
res-auth,」Container」;
res-sharing-scope,」Shareable」;
4.在web應用程序的context.xml中設置數據源鏈接,如下:
在<Context></Context>節點中加入,
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"/>
屬性說明:name,同第2步和第3步的屬性name值,和子節點res-ref-name值;
type,同樣取」javax.sql.DataSource」;
global,同name值。
至此,設置完成,下面是如何使用資料庫連接池。
1.建立一個連接池類,DBPool.java,用來創建連接池,代碼如下:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBPool {
private static DataSource pool;
static {
Context env = null;
try {
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource)env.lookup("jdbc/DBPool");
if(pool==null)
System.err.println("'DBPool' is an unknown DataSource");
} catch(NamingException ne) {
ne.printStackTrace();
}
}
public static DataSource getPool() {
return pool;
}
}
2.在要用到資料庫操作的類或jsp頁面中,用DBPool.getPool().getConnection(),獲得一個Connection對象,就可以進行資料庫操作,最後別忘了對Connection對象調用close()方法,注意:這里不會關閉這個Connection,而是將這個Connection放回資料庫連接池。 22723希望對你有幫助!
⑥ jsp中sql語句
確定在SQL 2000查詢分析器中,沒有問題嗎.那你就試試把查詢結果放到臨時表裡,再查詢排序?select ResumeNum,Favorites.ResumeName,TrueName,Sex,Mobile_Phone,School,Professional,Degree,Scoree,Statusinto #a from Favorites,Resume_Basic,Resume_Ecation,Resume_Language,Resume_Rewards_Punish where Favorites.ResumeName = Resume_Basic.UserName and Resume_Basic.UserName=Resume_Ecation.UserName and Resume_Rewards_Punish.UserName =Resume_Basic.UserName and Resume_Language.UserName= Resume_Basic.UserName and Resume_Ecation.ID='0001'and Favorites.CompanyNum='222' select * from #a order by Scoreedrop table #a----笨法子
⑦ jsp怎麼連接sql資料庫
1.將資料庫驅動程序的JAR文件放在Tomcat的 common/lib 中; 2.在server.xml中設置數據源,以MySQL資料庫為例,如下: 在 節點中加入, 屬性說明:name,數據源名稱,通常取」jdbc/XXX」的格式; type,」javax.sql.DataSource」; password,資料庫用戶密碼; driveClassName,資料庫驅動; maxIdle,最大空閑數,資料庫連接的最大空閑時間。超過空閑時間,資料庫連 接將被標記為不可用,然後被釋放。設為0表示無限制。 MaxActive,連接池的最大資料庫連接數。設為0表示無限制。 maxWait ,最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示 無限制。 3.在你的web應用程序的web.xml中設置數據源參考,如下: 在節點中加入, MySQL DB Connection Pool jdbc/DBPool javax.sql.DataSource Container Shareable 子節點說明: description,描述信息; res-ref-name,參考數據源名字,同上一步的屬性name; res-type,資源類型,」javax.sql.DataSource」; res-auth,」Container」; res-sharing-scope,」Shareable」; 4.在web應用程序的context.xml中設置數據源鏈接,如下: 在節點中加入, 屬性說明:name,同第2步和第3步的屬性name值,和子節點res-ref-name值; type,同樣取」javax.sql.DataSource」; global,同name值。 至此,設置完成,下面是如何使用資料庫連接池。 1.建立一個連接池類,DBPool.java,用來創建連接池,代碼如下: import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class DBPool { private static DataSource pool; static { Context env = null; try { env = (Context) new InitialContext().lookup("java:comp/env"); pool = (DataSource)env.lookup("jdbc/DBPool"); if(pool==null) System.err.println("'DBPool' is an unknown DataSource"); } catch(NamingException ne) { ne.printStackTrace(); } } public static DataSource getPool() { return pool; } } 2.在要用到資料庫操作的類或jsp頁面中,用DBPool.getPool().getConnection(),獲得一個Connection對象,就可以進行資料庫操作,最後別忘了對Connection對象調用close()方法,注意:這里不會關閉這個Connection,而是將這個Connection放回資料庫連接池。
⑧ java在jsp頁面如何直接執行sql
兩個簡單的jsp頁面,資料庫連接(我給你的是mysql資料庫連接示例,後面附sqlserver資料庫連接部分關鍵代碼)
首先是 獲取值頁面My.jsp 源碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'My.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Hp.jsp">
name:<input name="name" value="" type="text"></br>
password:<input name="password" value="" type="text"></br>
<input type="submit" value="button">
</form>
</body>
</html>
處理頁面 Hp.jsp 源碼:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Hp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Connection con = null;
Statement stm = null;
String url = "jdbc:mysql://localhost:3306/數據名稱";//資料庫名稱就是你的資料庫名字
String driver = "com.mysql.jdbc.Driver"; //驅動類位置
String username = "root"; //資料庫登錄名稱,此處寫上你的用戶名稱
String pwd = "root"; //資料庫登錄密碼,此處寫上你的登錄密碼
try
{
Class.forName(driver);
con = DriverManager.getConnection(url, username, pwd); //創建Connection連接對象
stm = con.createStatement(); //創建Statement 命令執行對象
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name=request.getParameter("name"); //獲取傳過來的名稱
String password=request.getParameter("password");//獲取傳過來的密碼
String sql="insert into user(name,password) values("+name+","+password+")";//資料庫添加一條記錄sql語句
int temp=stm.executeUpdate(sql);
if(temp>0)
{
out.print("添加成功");
}
else
{
out.print("添加失敗");
}
//關閉資料庫連接
stm.close();
con.close();
%>
</body>
</html>
注意 連接不同資料庫要導入不同的資料庫驅動包 你要導入才行啊
附 sqlserver資料庫連接 部分關鍵代碼:
private static Connection con = null;
private static Statement stm = null;
private static String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=資料庫名稱";
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//與mysql有所不同
private static String username = "sa";//默認用戶
private static String pwd = "123"; //密碼
static {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, pwd);
System.out.print("連接成功!");
stm = con.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}