Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jdbc to connect to different databases

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to use Jdbc to connect to different databases. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1.Jdbc connects to Access database

① select the system DNS tab through Control Panel > > Administrative tools > > data Source, add Microsoft Access Driver (* .mdb), and take the data source name sample as an example, select the database file. The link code in the program is as follows:

Public static Connection getConnection () {

Try {

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver")

Connection conn = DriverManager.getConnection ("jdbc:odbc:sample", "", "); / / this connection requires the establishment of a data source called sample

} catch (ClassNotFoundException e) {

E.printStackTrace ()

} catch (SQLException e) {

E.printStackTrace ()

}

Return conn

}

② establishes the connection directly in the program. The code is as follows:

Public static Connection getConnection () {

Try {

String url = "jdbc:odbc:driver= {Microsoft Access Driver (* .mdb)}; DBQ=D://Documents and Settings//Administrator//workspace//sample.mdb"

/ / the absolute path of the database to be given in url

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver")

Conn = DriverManager.getConnection (url, ",")

} catch (ClassNotFoundException e) {

E.printStackTrace ()

} catch (SQLException e) {

E.printStackTrace ()

}

Return conn

}

2.Jdbc connects to the MySQL database (requires a driver that introduces mysql)

Public static Connection getConnection () {

Try {

Class.forName ("com.mysql.jdbc.Driver")

Conn = DriverManager.getConnection (

"jdbc:mysql://localhost:3306/bookstore?user=root&password=&characterEncoding=utf-8"); / / bookstore is the name of your database

} catch (ClassNotFoundException e) {

E.printStackTrace ()

} catch (SQLException e) {

E.printStackTrace ()

}

Return conn

}

3.Jdbc connects to the Oracle database (requires classes12, a driver for Oracle)

Public static Connection getConnection () {

Try {

Class.forName ("oracle.jdbc.driver.OracleDriver")

Conn = DriverManager.getConnection (

"jdbc:oracle:thin:@localhost:1521:oracle9i", "scott", "tiger"); / / oracle9i is the database name

} catch (ClassNotFoundException e) {

E.printStackTrace ()

} catch (SQLException e) {

E.printStackTrace ()

}

Return conn

}

After reading the above, do you have any further understanding of how to use Jdbc to connect to different databases? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report