A driver for connecting MySQL and Sqlsever in JSP files and classes using the Java Web _java

Source: Internet
Author: User
Tags driver manager sql server driver mysql in java web

--method one uses the Java Web to connect to MySQL in a JSP file driver

<%@ 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 Bundles--> <%@ page import= "java.sql.*"%><!--Import all of the Java database resource bundles--> <% String path 
= Request.getcontextpath (); 
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

---Method one uses the Java Web to connect the connection sqlsever in a 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 Bundles--> <%@ page import= "java.sql.*"%><!--Import all of the Java database resource bundles--> <% String path 
= Request.getcontextpath (); 
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

---Method II using the Java Web to connect to the Sqlsever and MySQL drivers in the class file

public class Conndao {public Connection openconn () {Connection conn=null; 
try {//This is the database connection parameter object Class.forName ("Com.mysql.jdbc.Driver") connected to "MYSQL"; SQL Server link class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");/load Database driver, register to drive Manager//This is the connection "MYSQL" The database connection parameter object "mode One"/* Class.forName ("Com.mysql.jdbc.Driver"); 
Load MySQL driver. 
String url= "Jdbc:mysql://localhost:3306/db_database10"; 
String username= "Cherish memory hidden Butterflies"; 
String userpassword= "123"; Conn=drivermanager.getconnection (URL, username, userpassword);///"Mode II"//Class.forName (" 
Com.mysql.jdbc.Driver "); Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/db_database10", "Cherish Memory Butterfly", "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//Conn=drivermanager.getconnection (URL, username, userpasswo of MYSQL)RD); SQL Server link conn=drivermanager.getconnection ("Jdbc:sqlserver://localhost:1433;databasename=db_database10", " 
Cherish the memory of the Butterfly "," qwe199509060 "); 
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); 
Return conn; } 
}

Note: here to download a driver package My resources have MySQL and SQL Server driver package to download myself!

-----------------------------------------------------give a final canonical format test-------------------------------------------

Code is as follows, do not do more parsing:

Import java.sql.*; public class Dbconnectionmanager {//sqlserver private String drivername = "com.microsoft.sqlserver.jdbc.SQLServerDrive R "//load driver private string url =" Jdbc:microsoft:sqlserver://localhost:1433;databasename=master ";/Set database connection string private String user = "sa";//Database login user name private string password = "root";//Database login password private static string message = "Congratulations, database connection is normal!" 
"; 
public void Setdrivername (String newdrivername) {drivername = Newdrivername; 
Public String Getdrivername () {return drivername; 
The public void SetUrl (String newurl) {url = newurl; 
Public String GetUrl () {return URL; 
The 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 = "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 hint test variable private static String message = "Congratulations, database connection is normal!" 
"; Connection method public static Connection getconnection () {try {class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver") ;//Load Drive return drivermanager.getconnection ("Jdbc:microsoft:sqlserver://localhost:1433;databasename=master", "Cherish memory hidden disc "," qwe199509060 ")//implement connection parameter library name username 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 Case}}

The above is a small part of the introduction of the use of Java Web in JSP files and class to connect MySQL and Sqlsever drive method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.