Java access to MySQL and Oracle Link class _java

Source: Internet
Author: User

The examples in this article describe the Java-derived classes for MySQL and Oracle links. Share to everyone for your reference. as follows:

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException; public class Connectdb {private static final string MYSQL = "jdbc:mysql://"; private static final String ORACLE = "Jdbc:o
racle:thin:@ "; Private Connectdb () {} public static Connection getinstance (string DBType, String url) throws Nosuchdbexception, Sqlexcep tion {if ("MySQL". Equalsignorecase (DBType)) return getmysqlconn (URL); if ("Oracle" Equalsignorecase (DBType)) return
Getoracleconn (URL);
return null; public static void Closeconn (Connection conn) {if (conn!= null) {try {conn.close ();} catch (SQLException e) {E.pri
Ntstacktrace (); }} private static Connection getmysqlconn (String url) throws SQLException {Connection = null; try {conn
("Com.mysql.jdbc.Driver"); catch (ClassNotFoundException e) {e.printstacktrace ();} conn = drivermanager.getconnection (MYSQL + URL, "root", "root"
);
Return conn; private static Connection getoracleconn (String url) throws SQLException {
Connection conn = null; try {class.forname ("Oracle.jdbc.driver.OracleDriver");} catch (ClassNotFoundException e) {e.printstacktrace ();} conn
= drivermanager.getconnection (ORACLE + URL, "Scott", "Tiger");
Return conn;

 }
}

I hope this article will help you with your Java programming.

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.