激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - 使用java web 在jsp文件及Class中連接MySQL和SQLsever 的驅(qū)動(dòng)方法

使用java web 在jsp文件及Class中連接MySQL和SQLsever 的驅(qū)動(dòng)方法

2020-06-21 15:23惜憶隱蝶 JAVA教程

這篇文章主要介紹了使用java web 在jsp文件及Class中連接MySQL和SQLsever的驅(qū)動(dòng)方法的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下

--方法一 使用java web 在jsp文件中連接 連接MySQL的驅(qū)動(dòng)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Driver.*" %>
<%@page import="java.sql.SQLException"%>
<%@ page import="java.sql.Driver.*" %>
<%@ page import="java.util.*" %><!-- 導(dǎo)入所有的Java的資源包 -->
<%@ page import="java.sql.*"%><!-- 導(dǎo)入所有的Java數(shù)據(jù)庫的資源包 -->
<%
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 'index.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>
<%
try{
Class.forName("com.mysql.jdbc.Driver");//加載數(shù)據(jù)庫驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器
String URL="jdbc:mysql://localhost:3306/test";//數(shù)據(jù)庫連接字符串 localhost表示本機(jī)也可以用IP地址或者計(jì)算機(jī)的名字 3306表示服務(wù)端口 test表示數(shù)據(jù)庫的名稱
String username="惜憶隱蝶"; //數(shù)據(jù)庫用戶名
String password="123"; //數(shù)據(jù)庫密碼 123
// Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","惜憶隱蝶","123");//方式一
Connection cn=DriverManager.getConnection(URL, username, password);//方式二
//創(chuàng)建Connection連接
if(cn !=null){ //判斷數(shù)據(jù)庫連接是否成功
out.println("數(shù)據(jù)庫連接成功!"); //輸出連接信息
cn.close(); //關(guān)閉數(shù)據(jù)庫連接
}else{
out.println("數(shù)據(jù)庫連接失敗!"); //輸出連接信息
cn.close(); //關(guān)閉數(shù)據(jù)庫連接
}
}catch(ClassNotFoundException e){
e.printStackTrace();
out.println(e.toString()+"<br>驅(qū)動(dòng)類無法加載!");
}catch(SQLException e){
e.printStackTrace();
out.println(e.toString()+"<br>數(shù)據(jù)庫連接不上!");
}
%>
<br><br><br>
<form id="form1" name="form1" method="post" style="text-align:center" action="index1.jsp">
<input type="submit" name="Submit" value="連接SQL server" />
</form>
</body>
</html>

---方法一 使用java web 在jsp文件中連接 連接SQLsever的驅(qū)動(dòng)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Driver.*" %>
<%@page import="java.sql.SQLException"%>
<%@ page import="java.sql.Driver.*" %>
<%@ page import="java.util.*" %><!-- 導(dǎo)入所有的Java的資源包 -->
<%@ page import="java.sql.*"%><!-- 導(dǎo)入所有的Java數(shù)據(jù)庫的資源包 -->
<%
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 'index1.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>
<%
try{
Connection conn=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加載數(shù)據(jù)庫驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器
String URL="jdbc:sqlserver://localhost:1433;DataBaseName=msdb";//數(shù)據(jù)庫連接字符串 localhost表示本機(jī)也可以用IP地址或者計(jì)算機(jī)的名字 1433表示服務(wù)端口 DataBaseName=ConstructionDB或者DataBaseName=msdb表示數(shù)據(jù)庫的名稱
String username="sa"; //數(shù)據(jù)庫用戶名
String password="123"; //數(shù)據(jù)庫密碼 123
// conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=msdb","sa","123");//方式一
Connection cn=DriverManager.getConnection(URL, username, password);//方式二
//創(chuàng)建Connection連接
if(cn !=null){ //判斷數(shù)據(jù)庫連接是否成功
out.println("數(shù)據(jù)庫連接成功!"); //輸出連接信息
cn.close(); //關(guān)閉數(shù)據(jù)庫連接
}else{
out.println("數(shù)據(jù)庫連接失敗!"); //輸出連接信息
cn.close(); //關(guān)閉數(shù)據(jù)庫連接
}
}catch(ClassNotFoundException e){
e.printStackTrace();
out.println(e.toString()+"<br>驅(qū)動(dòng)類無法加載!");
}catch(SQLException e){
e.printStackTrace();
out.println(e.toString()+"<br>數(shù)據(jù)庫連接不上!");
}
%>
<br><br><br>
<form id="form1" name="form1" method="post" style="text-align:center" action="index.jsp">
<input type="submit" name="Submit" value="連接 My SQL" />
</form>
</body>
</html>

---方法二 使用java web 在Class文件中連接 連接SQLsever 和 MySQL的驅(qū)動(dòng)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class connDAO {
public Connection openconn()
{Connection conn=null;
try {
//這是連接【MYSQL】的數(shù)據(jù)庫連接參數(shù)對(duì)象
Class.forName("com.mysql.jdbc.Driver");
//【SQL server 鏈接】
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加載數(shù)據(jù)庫驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器
//這是連接【MYSQL】的數(shù)據(jù)庫連接參數(shù)對(duì)象 【方式一】
/* Class.forName("com.mysql.jdbc.Driver"); //加載Mysql驅(qū)動(dòng)。
String URL="jdbc:mysql://localhost:3306/db_database10";
String username="惜憶隱蝶";
String userpassword="123";
conn=DriverManager.getConnection(URL, username, userpassword);//建立連接
*/
// 【方式二】
// Class.forName("com.mysql.jdbc.Driver");
// conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_database10","惜憶隱蝶","123");//實(shí)行連接參數(shù) 庫名 用戶名 和密碼
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* String URL="jdbc:mysql://localhost:3306/db_database10";
String username="aa";
String userpassword="aa";
*/
try {
//這是連接【MYSQL】的數(shù)據(jù)庫連接參數(shù)對(duì)象
// conn=DriverManager.getConnection(URL, username, userpassword);
//【SQL server 鏈接】
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=db_database10", "惜憶隱蝶","qwe199509060");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}

注意:這里面要下載一個(gè)驅(qū)動(dòng)包 我的資源中有MySQL 和SQL server 的驅(qū)動(dòng)包 自己去下載!

-----------------------------------------------------給一個(gè)最終規(guī)范格式試題-------------------------------------------

代碼如下,不多做解析:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.sql.*;
public class DBConnectionManager {
//SQLServer
private String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//加載驅(qū)動(dòng)程序
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";//設(shè)置數(shù)據(jù)庫連接串
private String user = "sa";//數(shù)據(jù)庫登錄用戶名
private String password = "root";//數(shù)據(jù)庫登錄密碼
private static String message = "恭喜,數(shù)據(jù)庫連接正常!";
public void setDriverName(String newDriverName) {
driverName = newDriverName;
}
public String getDriverName() {
return driverName;
}
public void setUrl(String newUrl) {
url = newUrl;
}
public String getUrl() {
return url;
}
public void setUser(String newUser) {
user = newUser;
}
public String getUser() {
return user;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getPassword() {
return password;
}
public Connection getConnection() {
try {
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
message = "數(shù)據(jù)庫連接失敗!";
return null;
}
}
public static void main(String[] args) {
try{
DBConnectionManager dcm = new DBConnectionManager();
Connection conn = dcm.getConnection();
System.out.println(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
///第二種
package net.jiaxiang.Dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class Conn {
//定義提示 測試變量
private static String message = "恭喜,數(shù)據(jù)庫連接正常!";
//連接方法
public static Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加載驅(qū)動(dòng)
return DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master", "惜憶隱碟", "qwe199509060");//實(shí)行連接參數(shù) 庫名 用戶名 和密碼
} catch (Exception e) {
message = "數(shù)據(jù)庫連接失敗!";
e.printStackTrace();//打印異常
return null;
}
}
public static void main(String[] args) {
getConnection();//調(diào)用連接
System.out.println(message);//測試情況
}
}

以上所述是小編給大家介紹的使用java web 在jsp文件及Class中連接MySQL和SQLsever 的驅(qū)動(dòng)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/xiyiyindie/article/details/52764600

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 最近国产中文字幕 | 九九热精 | 精品久久久久久久久亚洲 | 一区二区三区欧美视频 | 久久久久亚洲精品国产 | 亚洲二区三区在线 | 天天碰天天操 | 欧美性受ⅹ╳╳╳黑人a性爽 | 亚洲性生活免费视频 | 亚洲福利视 | 久久久无码精品亚洲日韩按摩 | 成人永久免费 | 在线99热| av成人免费 | 欧美成人精品一区二区 | 天堂精品 | 久久99国产精品二区护士 | 午夜精品久久久久久久久久久久久蜜桃 | 在线观看精品视频 | 日本人乱人乱亲乱色视频观看 | 美国黄色小视频 | 国产无遮挡一区二区三区毛片日本 | 久久精品视频首页 | 日韩中文字幕一区二区三区 | 欧美一级α | 日韩欧美综合在线 | 男男啪羞羞视频网站 | 亚洲成人国产综合 | 一本免费视频 | 国产精品一区视频 | 午夜精品网站 | 福利免费在线观看 | 国产午夜免费不卡精品理论片 | 黄色7777| 一区二区三区四区高清视频 | 国产精品亚洲三区 | 欧美一区二区黄 | 国产中出视频 | 国产91精品久久久久久久 | 亚洲一区二区观看播放 | 精品黑人一区二区三区国语馆 |