Java连接各种数据库的方法
作者:丨深水蓝丨 时间:2024-01-28 10:56:26
本文实例讲述了Java连接各种数据库的方法。分享给大家供大家参考。具体如下:
//MySQL:
String Driver="com.mysql.jdbc.Driver"; //驱动程序
String URL="jdbc:mysql://localhost:3306/db_name"; //连接的URL,db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Microsoft SQL Server 2.0驱动(3个jar的那个):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
//Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
// Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password);
//PostgreSQL:
String Driver="org.postgresql.Driver"; //连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
// DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-//sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
// JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
希望本文所述对大家的java程序设计有所帮助。
标签:Java,连接,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql Explain 详解
2010-12-03 16:09:00
MySQL 修改密码实例详解
2024-01-27 05:36:28
![](https://img.aspxhome.com/file/2023/7/71047_0s.png)
vsCode安装使用教程和插件安装方法
2024-04-30 09:55:49
![](https://img.aspxhome.com/file/2023/7/132237_0s.jpg)
python opencv 检测移动物体并截图保存实例
2023-11-05 16:10:35
![](https://img.aspxhome.com/file/2023/9/63919_0s.jpg)
MySQL切分查询用法分析
2024-01-28 01:11:35
python爬虫之pyppeteer库简单使用
2023-05-29 14:07:20
![](https://img.aspxhome.com/file/2023/0/125210_0s.jpg)
基于python list对象中嵌套元组使用sort时的排序方法
2022-11-04 03:38:21
TensorFlow固化模型的实现操作
2022-09-12 22:28:42
Python之进行URL编码案例讲解
2023-03-31 03:46:58
![](https://img.aspxhome.com/file/2023/1/87761_0s.png)
想取消错误的mysql命令怎么办?
2024-01-13 07:39:38
详解MySQL 数据库优化方法
2010-08-12 14:50:00
SQL实战演练之网上商城数据库用户信息数据操作
2024-01-15 10:26:39
完美解决torch.cuda.is_available()一直返回False的玄学方法
2023-04-05 18:17:34
![](https://img.aspxhome.com/file/2023/8/128218_0s.jpg)
简化版的vue-router实现思路详解
2024-05-10 14:17:28
Matplotlib 折线图plot()所有用法详解
2023-01-30 06:10:30
![](https://img.aspxhome.com/file/2023/6/99566_0s.png)
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2021-04-02 02:05:02
![](https://img.aspxhome.com/file/2023/0/98870_0s.png)
postman传递当前时间戳实例详解
2022-01-09 17:28:34
![](https://img.aspxhome.com/file/2023/6/133456_0s.png)
MySQL服务器 IO 100%的分析与优化方案
2024-01-24 08:34:10
![](https://img.aspxhome.com/file/2023/3/118483_0s.png)
php中json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案
2023-09-27 17:22:32
Python处理mysql数据库
2010-12-03 16:23:00