jspmysql資料庫
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資料庫中的數據
❷ jsp 怎樣連接mysql資料庫
(1)把mysql的驅動放到tomcat的lib中 驅動是這個
http://ftp.up.ac.za/pub/windows/MySQL/Downloads/Connector-J/mysql-connector-java-5.1.6.zip
解壓後在lib中有mysql-connector-java-5.1.6.jar.把這個文件放到tomcat的lib中5.X的在tomcat/common/lib 6.0在tomcat/lib
(2)建一個很簡單的表person就兩個欄位username和password,資料庫名和資料庫密碼換成你的就是了
create database ibatis;--創建資料庫
use ibatis;--使用資料庫,以下表在該資料庫中
create table person(username varchar(20),password varchar(20));--創建person表
(3)創建index.jsp和regist.jsp
1:
index.jsp 提交表單頁面
<%@ page pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<form action="regist.jsp" method="post">
username :<input type = "text" name="name"/>
password :<input type = "password" name="password"/>
<input type = "submit" value="提交"/>
</form>
</body>
</html>
2:regist.jsp //用戶注冊同時顯示所有用戶
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<body>
<center>
<%
request.setCharacterEncoding("GBK");
String uname=request.getParameter("name"); //從表單獲得
String pwd=request.getParameter("password"); //從表單獲得
String driver="com.mysql.jdbc.Driver"; //我用的是mysql官方驅動你自己換一下就是了 在這里有
String url="jdbc:mysql://localhost:3306/ibatis?user=root&password=yanghao"; //這是資料庫連接地址Ibatis是資料庫名稱,user是用戶.password就是你的用戶名,根據實際情況你修改
String sql="INSERT INTO person (username,password) VALUES('"+uname+"','"+pwd+"')"; //把index.jsp提交的兩個數據插進資料庫的資料庫語句
Connection conn=null; //資料庫連接
Statement stmt=null;
ResultSet rs = null; //查詢結果
%>
<%
Class.forName(driver); //載入驅動
conn=DriverManager.getConnection(url); //獲得連接
stmt=conn.createStatement();
stmt.execute(sql);//存入資料庫
rs=stmt.executeQuery("select * from person"); //查詢所有person語句
%>
<%
if(rs!=null){ //判斷以下
while(rs.next()){
String username=rs.getString(1);
String password=rs.getString(2);
%>
<table>
<tr>
<td><%=username %></td>
<td><%=password %></td>
</tr>
</table>
<%
//關閉資料庫連接,和開始的順序是反的
rs.close();//關閉結果集
stmt.close();//關閉Statement
conn.close();//關閉資料庫連接
//ok完成了插入和查詢操作
}
}
%>
</center>
</body>
❸ jsp中使用JDBC連接MySQL資料庫如何解決
在index.jsp中輸入如下代碼,並配置相應mySQL資料庫數據
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<body>
<%
String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的資料庫名test1
String url = "jdbc:mysql://127.0.0.1:3306/test";
// MySQL配置時的用戶名
String user = "root";
// Java連接MySQL配置時的密碼
String password = "111";
try {
// 1 載入驅動程序
Class.forName(driver);
// 2 連接資料庫
Connection conn = DriverManager.getConnection(url, user, password);
// 3 用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "select * from login";
ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) {
name = rs.getString("userName");
mima = rs.getString("passWord");
out.println(name+"\t"+mima);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
12,這樣就運行成功了,對於出現8080埠號被佔用,可以採用如下的方法進行刪除對應的進程。
在命令提示符下,輸入netstat -aon | findstr 8080
找到對應的進程的PID,假設是7659 再輸入如下的命令
taskkill /pid 7659 /F
即可刪除對應的進程。
❹ jsp怎麼從mysql資料庫把樹形結構展現出來
jsp從mysql資料庫讀取數據,並填充到樹形結構菜單並展現出來的實現方法:
1、引入jquery.treeview.js樹控制項
<script type="text/javascript" src="jquery/easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery/easyui/jquery.easyui.min.js"></script>
2、jsp頁面中獲取後台mysql數據,並傳到jsp頁面來
<%
// 資料庫的名字
String dbName = "zap";
// 登錄資料庫的用戶名
String username = "sa";
// 登錄資料庫的密碼
String password = "123";
// 資料庫的IP地址,本機可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1";
// 資料庫的埠,一般不會修改,默認為1433
int port = 1433;
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username
+ ";password=" + password;
//
//聲明需要使用的資源
// 資料庫連接,記得用完了一定要關閉
Connection con = null;
// Statement 記得用完了一定要關閉
Statement stmt = null;
// 結果集,記得用完了一定要關閉
ResultSet rs = null;
try {
// 注冊驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 獲得一個資料庫連接
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT * from note";
// 創建查詢
stmt = con.createStatement();
// 執行查詢,拿到結果集
rs = stmt.executeQuery(SQL);
while (rs.next()) {
%>
<tr>
3、填充樹形菜單:
{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
$('#tree').jstree({
'core' : {
'data' : function (obj, cb) {
cb.call(this,
['Root 1', 'Root 2']);
}
}});
❺ 請問怎麼用jsp連接mysql資料庫.
1.先裝好JDKex:C:\j2sdk1.4.2
2.再裝TomCat ex:C:\Tomcat 5.0
3.下載JDBC 驅動
http://mysql.easynet.be/Downloads/Connector-J/mysql-connector-java-3.1.14.zip
下載就直接解壓,裡面有一個mysql-connector-java-3.1.14-bin.jar文件
在 C:\建立一個mysqlforjdbc文件夾,將mysql-connector-java-3.1.14-bin.jar復制到這個文件夾中.
再將mysql-connector-java-3.1.14-bin.jar復制到C:\j2sdk1.4.2\lib中
再將mysql-connector-java-3.1.14-bin.jar復制到C:\Tomcat 5.0\common\lib和C:\Tomcat 5.0\shared\lib中
4.設置環境變數
classpath .;d:\j2sdk\lib\dt.jar;d:\j2sdk\lib\tools.jar;d:\mysqlforjdbc\mysql-connector-java-3.1.14-bin.jar;d:\j2sdk\lib\mysql-connector-java-3.1.14-bin.jar;d:\mysqlforjdbc\msbase.jar;d:\mysqlforjdbc\mssqlserver.jar;d:\mysqlforjdbc\msutil.jar;%CATALINA_HOME%\common\lib\servlet.jar
JAVA_HOME d:\j2sdk
Path ;%JAVA_HOME%\bin\
5.執行代碼,輸出結果在C:\Tomcat 5.0\webapps\ROOT中新建test_mysql.jsp測試文件<%@ page contentType="text/html; charset=gb2312" %><%@ page language="java" %><%@ page import="com.mysql.jdbc.Driver" %><%@ page import="java.sql.*" %>
<%//驅動程序名String driverName="com.mysql.jdbc.Driver";//資料庫用戶名String userName="root";//密碼String userPasswd="";//資料庫名String dbName="ex";//表名String tableName="post";//聯結字元串String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;Class.forName("com.mysql.jdbc.Driver").newInstance();Connection connection=DriverManager.getConnection(url);Statement statement = connection.createStatement();String sql="SELECT * FROM "+tableName;ResultSet rs = statement.executeQuery(sql); //獲得數據結果集合ResultSetMetaData rmeta = rs.getMetaData();//確定數據集的列數,亦欄位數int numColumns=rmeta.getColumnCount();
out.print("id"); out.print("| ");out.print("title");out.print("| ");out.print("comment ");out.print("<br>");while(rs.next()) {out.print(rs.getString(1)); out.print(" | ");out.print(rs.getString(2));out.print(" | "); out.print(rs.getString(3));out.print("<br>"); } out.print("<br>");out.print("資料庫操作成功,恭喜你"); rs.close(); statement.close(); connection.close(); %>
❻ 如何在JSP頁面顯示mysql資料庫內容
顯示資料庫數據的jsp代碼如下:
解釋及說明在代碼的注釋中即可查看
<spanstyle="font-size:12px;"><spanstyle="font-size:14px;"><%@pagelanguage="java"import="java.sql.*,java.io.*,java.util.*"%>
<%@pagecontentType="text/html;charset=utf-8"%>
<html>
<head>
<styletype="text/css">
table{
border:2px#CCCCCCsolid;
width:360px;
}
td,th{
height:30px;
border:#CCCCCC1pxsolid;
}
</style>
</head>
<body>
<%
//驅動程序名
StringdriverName="com.mysql.jdbc.Driver";
//資料庫用戶名
StringuserName="root";
//密碼
StringuserPasswd="szy";
//資料庫名
StringdbName="studentmanage";
//表名
StringtableName="student";
//連接字元串
Stringurl="jdbc:mysql://資料庫地址:埠號/"+dbName+"?user="
+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connectionconnection=DriverManager.getConnection(url);
Statementstatement=connection.createStatement();
Stringsql="SELECT*FROM"+tableName;
ResultSetrs=statement.executeQuery(sql);
%>
<br>
<br>
<tablealign="center">
<tr>
<th>
<%
out.print("學號");
%>
</th>
<th>
<%
out.print("姓名");
%>
</th>
<th>
<%
out.print("專業");
%>
</th>
<th>
<%
out.print("班級");
%>
</th>
</tr>
<%
while(rs.next()){
%>
<tr>
<td>
<%
out.print(rs.getString(1));
%>
</td>
<td>
<%
out.print(rs.getString(2));
%>
</td>
<td>
<%
out.print(rs.getString(3));
%>
</td>
<td>
<%
out.print(rs.getString(4));
%>
</td>
</tr>
<%
}
%>
</table>
<divalign="center">
<br><br><br>
<%
out.print("數據查詢成功,恭喜你");
%>
</div>
<%
rs.close();
statement.close();
connection.close();
%>
</body>
</html></span><spanstyle="font-size:24px;color:rgb(255,0,0);">
</span></span>
顯示結果如下所示: