JDBC Connection Database (mysql,sqlserver,oracle)

Source: Internet
Author: User

 Packagecom.test;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classDbconn { Public StaticConnection Getconn (String db) {string drivername=""; String Dburl=""; String UserName= ""; String userpwd = "";if(Db.equals ("SQL Server") ) {drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Dburl= "jdbc:sqlserver://192.168.0.1:1444; Databasename=test "; UserName= ""; Userpwd= ""; }Else if(Db.equals ("Oracle") ) {drivername= "Oracle.jdbc.driver.OracleDriver"; Dburl= "Jdbc:oracle:thin:@192.168.0.1:1521:test"; UserName= ""; Userpwd= ""; }Else if(Db.equals ("MySQL") ) {drivername= "Com.mysql.jdbc.Driver"; Dburl= "Jdbc:mysql://192.168.0.1:3306/test"; UserName= ""; Userpwd= ""; } Connection dbconn=NULL; Try{class.forname (drivername); Dbconn=drivermanager.getconnection (Dburl, UserName, userpwd); } Catch(Exception e) {e.printstacktrace (); }        returnDbconn; }         Public Static voidMain (string[] args) {Connection dbconn=dbconn.getconn ("Oracle");        System.out.println (Dbconn); String SQL= "SELECT id from table";        PreparedStatement PS; Try{PS=dbconn.preparestatement (SQL); ResultSet RS=Ps.executequery ();  while(Rs.next ()) System.out.println (Rs.getint ("id")); } Catch(SQLException e) {e.printstacktrace (); }    }}

JDBC Connection Database (mysql,sqlserver,oracle)

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.