没有合适的资源?快使用搜索试试~ 我知道了~
首页三种数据库连接的配置XML文件
资源详情
资源推荐
==================TestSQLServer.java 连接 SQLServer========================
import java.sql.*;
import java.util.*;
public class TestSQLServer{
public static void main (String args[]){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb";
String user ="sa";
String password = "sa";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM users");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
=================TestMysql.java 连接 mysql 数据库===========================
package org.binbo.dom;
import java.sql.*;
public class TestMysql{
public static void main (String args[]){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/binbo";
String user ="root";
String password = "binbo";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM testxml");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
=================TestOracle.java 连接 oracle 数据库==================
package org.binbo.dom;
import java.sql.*;
public class TestOracle{
public static void main (String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:binbo";
String user ="scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM test");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
===============XML 连接数据库=========================================
da.xml
<?xml version="1.0" encoding="UTF-8"?>
<PEOPLE><!--
<PERSON PERSONID="E01">
<className>com.microsoft.jdbc.sqlserver.SQLServerDriver</className>
<url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=binbo</url>
<user>sa</user>
<password>sa</password>
</PERSON>
--><PERSON PERSONID="E02">
<className>com.mysql.jdbc.Driver</className>
<url>jdbc:mysql://localhost:3306/binbo</url>
<user>root</user>
<password>binbo</password>
</PERSON><!--
<PERSON PERSONID="E03">
<className>oracle.jdbc.driver.OracleDriver</className>
<url>jdbc:oracle:thin:@localhost:1521:binbo</url>
<user>scott</user>
<password>tiger</password>
</PERSON>
--></PEOPLE>
--------------------------------------------
content.java
package org.binbo.dom;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Content {
public static Connection getConnection(){
Connection conn =null;
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("da.xml");
NodeList nl=doc.getElementsByTagName("PERSON");
Element node=(Element) nl.item(0);
String className
=node.getElementsByTagName("className").item(0).getFirstChild().getNodeValue();
String url =node.getElementsByTagName("url").item(0).getFirstChild().getNodeValue();
String user =node.getElementsByTagName("user").item(0).getFirstChild().getNodeValue();
String pwd
=node.getElementsByTagName("password").item(0).getFirstChild().getNodeValue();
Class.forName(className);
剩余16页未读,继续阅读
benben1015518
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- GO婚礼设计创业计划:技术驱动的婚庆服务
- 微信行业发展现状及未来发展趋势分析
- 信息技术在教育中的融合与应用策略
- 微信小程序设计规范:友好、清晰的用户体验指南
- 联鼎医疗:三级甲等医院全面容灾备份方案设计
- 构建数据指标体系:电商、社区、金融APP案例分析
- 信息技术:六年级学生制作多媒体配乐古诗教程
- 六年级学生PowerPoint音乐动画实战:制作配乐古诗演示
- 信息技术教学设计:特点与策略
- Word中制作课程表:信息技术教学设计
- Word教学:制作课程表,掌握表格基础知识
- 信息技术教研活动年度总结与成果
- 香格里拉旅游网设计解读:机遇与挑战并存
- 助理电子商务师模拟试题:设计与技术详解
- 计算机网络技术专业教学资源库建设与深圳IT产业结合
- 微信小程序开发:网络与媒体API详解
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功