JDBC access to various database connections __ Database

Source: Internet
Author: User
Tags odbc odbc connection stmt
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import java.sql.Statement;
	public class Connectionsmanager {Connection conn;
	String user = "Dada";

	String password = "Dandan"; /** * Acquisition of Oracle connection * @return * @throws Exception/public Connection Getoracleconn () throws Exception {String
		url = "Jdbc:oracle:thin:@127.0.0.1:1521:dandan";
		Class.forName ("Oracle.jdbc.driver.OracleDriver");
		conn = drivermanager.getconnection (URL, user, password);
	Return conn;  /** * Get MySQL connection * @return * @throws Exception/public Connection Getmysqlconn () throws Exception {String
		url = "Jdbc:mysql://127.0.0.1/dandan";
		Class.forName ("Org.gjt.mm.mysql.Driver");
		conn = drivermanager.getconnection (URL, user, password);
	Return conn;
		/** * Get SQL Server connection * @return * @throws Exception/public Connection Getsqlserverconn () throws Exception { String url = "Com.microsoft.jdbc.sqlserver.SQLServerdriver ";
		Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");

		conn = drivermanager.getconnection (URL, user, password);
	Return conn; /** * Get ODBC connection * @return * @throws Exception/public Connection Getodbcconn () throws Exception {String u
		RL = "Jdbc:odbc:ExcelODBC1";
		Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
		conn = drivermanager.getconnection (URL, user, password);

		Return conn;
		It should be noted that when querying using ODBC, there are some differences with standard SQL//need to use "[]" to enclose the name of the table to be queried//Statement stmt=con.createstatement ();
	ResultSet rs=stmt.executequery ("select * from [sheet1$]"); /** * Get access connection * @return * @throws Exception/public Connection Getaccessconn () throws Exception {Stri
		ng url = "Jdbc:odbc:test";
		Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
		conn = drivermanager.getconnection (URL, user, password);
	Return conn; /** * Get SQL Server connection through ODBC * @return * @throws Exception/public Connection getSqlServerConn1 () throws Exception {String url = ' JDBC:ODBC:MYDB1 ';
		Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
		conn = drivermanager.getconnection (URL, user, password);
		Connection con =//Drivermanager.getconnection ("Jdbc:odbc:mydb1;database=pubs", "", "");
	Return conn; /** * Access to connection via OCI * * * This way only to get a connection to Oracle * @return * @throws Exception * * Public Connection getconnbyoci () th
		Rows Exception {class.forname ("oracle.jdbc.driver.OracleDriver");
		Connection conn =//Drivermanager.getconnection ("Jdbc:oracle:oci: @dandan", "Root", "Dada"); Connection conn = DriverManager. getconnection (jdbc:oracle:oci:@ (description= (address=) host=127.0.0.1
		L=TCP) (port=1521)) (Connect_data= (SID=ORA9)) "," Scott "," Tiger ");
	Return conn;
 }
}


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.