使用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 {
//定義提示 測試變量
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和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 異步編程實(shí)踐_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
異步編程提供了一個(gè)非阻塞的,事件驅(qū)動(dòng)的編程模型。下面通過本文給大家介紹Java 異步編程實(shí)踐,感興趣的的朋友一起看看吧2017-05-05
Servlet3.0學(xué)習(xí)總結(jié)之基于Servlet3.0的文件上傳實(shí)例
本篇文章主要介紹了Servlet3.0學(xué)習(xí)總結(jié)之基于Servlet3.0的文件上傳實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Java設(shè)計(jì)模式之訪問模式(Visitor者模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之訪問模式(Visitor者模式)介紹,本文講解了為何使用Visitor模式、如何使用Visitor模式、使用Visitor模式的前提等內(nèi)容,需要的朋友可以參考下2015-03-03
詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)
大家應(yīng)該都知道請(qǐng)求重定向和請(qǐng)求轉(zhuǎn)發(fā)都是web開發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
Mybatis動(dòng)態(tài)sql超詳細(xì)講解
動(dòng)態(tài)SQL是MyBatis的強(qiáng)大特性之一,顧名思義就是會(huì)動(dòng)的SQL,即是能夠靈活的根據(jù)某種條件拼接出完整的SQL語句,下面這篇文章主要給大家介紹了關(guān)于Mybatis動(dòng)態(tài)sql的相關(guān)資料,需要的朋友可以參考下2023-04-04
SpringBoot 使用Mybatis分頁插件實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot 使用Mybatis分頁插件實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
java設(shè)計(jì)模式之單例模式的詳解及優(yōu)點(diǎn)
這篇文章主要介紹了java設(shè)計(jì)模式之單例模式的詳解及優(yōu)點(diǎn)的相關(guān)資料,如果一個(gè)類始終只能創(chuàng)建一個(gè)實(shí)例,那么這個(gè)類被稱為單例類,這種設(shè)計(jì)模式被稱為單例模式,需要的朋友可以參考下2017-08-08
Java實(shí)現(xiàn)的貸款金額計(jì)算功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的貸款金額計(jì)算功能,結(jié)合實(shí)例形式分析了Java簡單數(shù)值運(yùn)算及類型轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

