--Method 1 Use java web to connect to MySQL driver in jsp file
<%@ 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.*" %><!-- Import all Java resource packages--> <%@page import="java.sql.*"%><!-- Import all Java database resource packages--> <% 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");//Load the database driver and register it with the driver manager String URL="jdbc:mysql://localhost:3306/test";//Database connection string localhost means that the machine can also use the IP address or the computer name 3306 to represent the service port test to represent the database name String username="Xiyi Yindie"; //Database username String password="123"; //Database password 123 //Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","Xiyi I Memories Hidden Butterfly","123");//Method 1 Connection cn=DriverManager.getConnection(URL, username, password);//Method 2//Create Connection connection if(cn !=null){ //Judge whether the database connection is successful out.println("Database connection is successful! "); //Output connection information cn.close(); //Close the database connection}else{ out.println("Database connection failed!"); //Output connection information cn.close(); //Close the database connection} }catch(ClassNotFoundException e){ e.printStackTrace(); out.println(e.toString()+"<br>Driver class cannot be loaded!"); } catch(SQLException e){ e.printStackTrace(); out.println(e.toString()+"<br>Database connection cannot be loaded!"); } %> <br><br><br><br> <form id="form1" name="form1" method="post" style="text-align:center" action="index1.jsp"> <input type="submit" name="Submit" value="Connect SQL server" /> </form> </body> </html>---Method 1 Use java web to connect to SQLsever driver in jsp file
<%@ 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.*" %><!-- Import all Java resource packages--> <%@page import="java.sql.*"%><!-- Import all Java database resource packages--> <% 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");//Load the database driver and register it with the driver manager String URL="jdbc:sqlserver://localhost:1433;DataBaseName=msdb";//Database connection string localhost means that the machine can also use the IP address or the name of the computer 1433 to represent the service port DataBaseName=ConstructionDB or DataBaseName=msdb represents the name of the database String username="sa"; //Database username String password="123"; //Database password 123 // conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=msdb","sa","123");//Method 1 Connection cn=DriverManager.getConnection(URL, username, password);//Method 2 //Create Connection connection if(cn !=null){ //Judge whether the database connection is successful out.println("Database connection is successful! "); //Output connection information cn.close(); //Close the database connection}else{ out.println("Database connection failed!"); //Output connection information cn.close(); //Close the database connection} }catch(ClassNotFoundException e){ e.printStackTrace(); out.println(e.toString()+"<br>Driver class cannot be loaded!"); } catch(SQLException e){ e.printStackTrace(); out.println(e.toString()+"<br>Database connection cannot be loaded!"); } %> <br><br><br><br> <form id="form1" name="form1" method="post" style="text-align:center" action="index.jsp"> <input type="submit" name="Submit" value="Connection My SQL" /> </form> </body> </html>---Method 2 Use java web to connect the driver that connects SQLsever and MySQL in Class files
public class connDAO { public Connection openconn() {Connection conn=null; try { //This is the database connection parameter object connected to [MYSQL] Class.forName("com.mysql.jdbc.Driver"); //【SQL server link】 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //Load the database driver and register it with the driver manager//This is the database connection parameter object connected to [MYSQL] [Method 1] /* Class.forName("com.mysql.jdbc.Driver"); //Load the Mysql driver. String URL="jdbc:mysql://localhost:3306/db_database10"; String username="Xiyi Yindian"; String userpassword="123"; conn=DriverManager.getConnection(URL, username, userpassword);// Establish a connection*/ // [Method 2] // Class.forName("com.mysql.jdbc.Driver"); // conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_database10","Xiyiyindie","123");//Implement connection parameter library name username and password} 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 { //This is the database connection parameter object connecting to [MYSQL] // conn=DriverManager.getConnection(URL, username, userpassword); //【SQL server link】 conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=db_database10", "Sui I Memories Hidden Butterfly","qwe199509060"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } }Note: Here you need to download a driver package. My resources include MySQL and SQL server driver packages. Download it yourself!
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The code is as follows, no more analysis is done:
import java.sql.*; public class DBConnectionManager { //SQLServer private String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//Load the driver private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";//Set the database connection string private String user = "sa";//Database login user name private String password = "root";//Database login password private static String message = "Congratulations, the database connection is normal!"; 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 = "The database connection failed! "; 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(); } } } ///The second package net.jiaxiang.Dao; import java.sql.Connection; import java.sql.DriverManager; public class Conn { //Definition prompts test variable private static String message = "Congratulations, the database connection is normal! "; //Connection method public static Connection getConnection(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//Load driver return DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master", "Xiyiyindian", "qwe199509060");//Implement connection parameter library name user name and password} catch (Exception e) { message = "Database connection failed! "; e.printStackTrace();//Print exception return null; } } public static void main(String[] args) { getConnection();//Call connection System.out.println(message);//Test situation} }The above is the driver method of using java web to connect MySQL and SQLsever in jsp files and Class. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!