最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年09月18日 10:05:55   作者:惜憶隱蝶  
這篇文章主要介紹了使用java web 在jsp文件及Class中連接MySQL和SQLserver的驅(qū)動(dòng)方法的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下

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

<%@ 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ù)庫(kù)的資源包 --> 
<% 
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ù)庫(kù)驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器 
String URL="jdbc:mysql://localhost:3306/test";//數(shù)據(jù)庫(kù)連接字符串 localhost表示本機(jī)也可以用IP地址或者計(jì)算機(jī)的名字 3306表示服務(wù)端口 test表示數(shù)據(jù)庫(kù)的名稱 
String username="惜憶隱蝶"; //數(shù)據(jù)庫(kù)用戶名 
String password="123"; //數(shù)據(jù)庫(kù)密碼 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ù)庫(kù)連接是否成功 
out.println("數(shù)據(jù)庫(kù)連接成功!"); //輸出連接信息 
cn.close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 
}else{ 
out.println("數(shù)據(jù)庫(kù)連接失?。?); //輸出連接信息 
cn.close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 
} 
}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ù)庫(kù)連接不上!"); 
} 
%> 
<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)

<%@ 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ù)庫(kù)的資源包 --> 
<% 
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ù)庫(kù)驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器 
String URL="jdbc:sqlserver://localhost:1433;DataBaseName=msdb";//數(shù)據(jù)庫(kù)連接字符串 localhost表示本機(jī)也可以用IP地址或者計(jì)算機(jī)的名字 1433表示服務(wù)端口 DataBaseName=ConstructionDB或者DataBaseName=msdb表示數(shù)據(jù)庫(kù)的名稱 
String username="sa"; //數(shù)據(jù)庫(kù)用戶名 
String password="123"; //數(shù)據(jù)庫(kù)密碼 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ù)庫(kù)連接是否成功 
out.println("數(shù)據(jù)庫(kù)連接成功!"); //輸出連接信息 
cn.close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 
}else{ 
out.println("數(shù)據(jù)庫(kù)連接失?。?); //輸出連接信息 
cn.close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 
} 
}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ù)庫(kù)連接不上!"); 
} 
%> 
<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)

public class connDAO { 
public Connection openconn() 
{Connection conn=null; 
try { 
//這是連接【MYSQL】的數(shù)據(jù)庫(kù)連接參數(shù)對(duì)象 
Class.forName("com.mysql.jdbc.Driver"); 
//【SQL server 鏈接】 
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加載數(shù)據(jù)庫(kù)驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器 
//這是連接【MYSQL】的數(shù)據(jù)庫(kù)連接參數(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ù) 庫(kù)名 用戶名 和密碼 
} 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ù)庫(kù)連接參數(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ī)范格式試題-------------------------------------------

代碼如下,不多做解析:

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ù)庫(kù)連接串 
private String user = "sa";//數(shù)據(jù)庫(kù)登錄用戶名 
private String password = "root";//數(shù)據(jù)庫(kù)登錄密碼 
private static String message = "恭喜,數(shù)據(jù)庫(kù)連接正常!"; 
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ù)庫(kù)連接失?。?; 
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 { 
//定義提示 測(cè)試變量 
private static String message = "恭喜,數(shù)據(jù)庫(kù)連接正常!"; 
//連接方法 
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ù) 庫(kù)名 用戶名 和密碼 
} catch (Exception e) { 
message = "數(shù)據(jù)庫(kù)連接失??!"; 
e.printStackTrace();//打印異常 
return null; 
} 
} 
public static void main(String[] args) { 
getConnection();//調(diào)用連接 
System.out.println(message);//測(cè)試情況 
} 
} 

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

相關(guān)文章

最新評(píng)論

霍邱县| 凤阳县| 原阳县| 古蔺县| 顺义区| 沂南县| 无极县| 绥芬河市| 佛学| 安陆市| 海丰县| 盐城市| 遂溪县| 巫溪县| 灵台县| 梨树县| 诸城市| 邻水| 西畴县| 福鼎市| 砚山县| 神农架林区| 台江县| 张北县| 阜平县| 无极县| 香格里拉县| 佳木斯市| 响水县| 肃北| 宝应县| 布尔津县| 乐陵市| 四子王旗| 嘉义县| 合作市| 易门县| 永靖县| 兰溪市| 马公市| 阳新县|