使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動(dòng)方法
--方法一 使用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ù)庫的資源包 -->
<%
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)
<%@ 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)
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ī)范格式試題-------------------------------------------
代碼如下,不多做解析:
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 {
//定義提示 測(cè)試變量
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);//測(cè)試情況
}
}
以上所述是小編給大家介紹的使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動(dòng)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- java中jdbcTemplate的queryForList(坑)
- Java Spring5學(xué)習(xí)之JdbcTemplate詳解
- Java 使用JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤解決辦法
- Java連接sqlserver2008數(shù)據(jù)庫代碼
- JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實(shí)例代碼
- 用Java連接sqlserver數(shù)據(jù)庫時(shí)候幾個(gè)jar包的區(qū)別分析
- java sqlserver text 類型字段讀取方法
- Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫的方法
相關(guān)文章
Java輕松掌握面向?qū)ο蟮娜筇匦苑庋b與繼承和多態(tài)
本文主要講述的是面向?qū)ο蟮娜筇匦裕悍庋b,繼承,多態(tài),內(nèi)容含括從封裝到繼承再到多態(tài)的所有重點(diǎn)內(nèi)容以及使用細(xì)節(jié)和注意事項(xiàng),內(nèi)容有點(diǎn)長,請(qǐng)大家耐心看完2022-05-05
JDK10新特性之var泛型和多個(gè)接口實(shí)現(xiàn)方法
這篇文章主要介紹了JDK10的新特性:var泛型和多個(gè)接口實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
java list中包含某個(gè)字符串的兩種方法實(shí)現(xiàn)
在Java開發(fā)中,經(jīng)常需要判斷一個(gè)List中是否包含特定的字符串,包括使用contains()方法和循環(huán)遍歷判斷,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Java詳細(xì)分析String類與StringBuffer和StringBuilder的使用方法
當(dāng)對(duì)字符串進(jìn)行修改的時(shí)候,需要使用 StringBuffer 和 StringBuilder類,和String類不同的是,StringBuffer和 StringBuilder類的對(duì)象能夠被多次的修改,并且不產(chǎn)生新的未使用對(duì)象2022-04-04
java實(shí)體對(duì)象與Map之間的轉(zhuǎn)換工具類代碼實(shí)例
這篇文章主要介紹了java實(shí)體對(duì)象與Map之間的轉(zhuǎn)換工具類代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SpringBoot使用WebSocket實(shí)現(xiàn)向前端推送消息功能
WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議,它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工(full-duplex)通信——允許服務(wù)器主動(dòng)發(fā)送信息給客戶端,本文給大家介紹了SpringBoot使用WebSocket實(shí)現(xiàn)向前端推送消息功能,需要的朋友可以參考下2024-05-05
SpringMVC深入講解文件的上傳下載實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了springMVC實(shí)現(xiàn)文件上傳和下載的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
JavaCV使用ffmpeg實(shí)現(xiàn)錄屏功能
這篇文章主要介紹了JavaCV如何使用ffmpeg實(shí)現(xiàn)錄屏功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
spring-boot-maven-plugin報(bào)紅解決方案(親測(cè)有效)
本文主要介紹了spring-boot-maven-plugin報(bào)紅解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Java IO復(fù)用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java IO復(fù)用的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-05-05

