`

Java连接各种数据库,DB2、Mysql、Oracle、Hsqldb。。

阅读更多
ConnectHSQLDB.java
/**
 * ConnectHSQLDB.java
 */
package com.javaworkspace.connecthsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectHSQLDB {
	public static void main(String[] args) {

		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("org.hsqldb.jdbcDriver");
			connection = DriverManager.getConnection(
					"jdbc:hsqldb:file:C:\\HyperSQLDB\\EMPLOYEEDB", "SA", "");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


ConnectMySQL.java
/**
 * ConnectMySQL.java
 */
package com.javaworkspace.connectmysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectMySQL {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/EMPLOYEE", "root", "root");
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


Connect MS SQL Server
/**
 * ConnectMSSQL.java
 */
package com.javaworkspace.connectmssql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectMSSQL {
	public static void main(String[] args) {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Connection connection = DriverManager
					.getConnection(
							"jdbc:sqlserver://localhost:1433;databaseName=EMPLOYEE;selectMethod=cursor",
							"username", "password");

			System.out.println("DATABASE NAME IS:"
					+ connection.getMetaData().getDatabaseProductName());
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


// Connect using JDTS driver
/**
 * ConnectMSSQL.java
 */
package com.javaworkspace.connectmssql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectMSSQL {
	public static void main(String[] args) {
		try {
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			Connection connection = DriverManager
					.getConnection(
							"jdbc:jtds:sqlserver://localhost:1433;databaseName=EMPLOYEE;sendStringParametersAsUnicode=false",
							"username", "password");
			System.out.println("DATABASE NAME IS:"
					+ connection.getMetaData().getDatabaseProductName());
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



Connect Oracle
/**
 * ConnectOracle.java
 */
package com.javaworkspace.connectoracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectOracle {

	public static void main(String[] args) {

		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			System.out.println("Connecting to the database...");
			Connection connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
			Statement statement = connection.createStatement();
			ResultSet resultset = statement.executeQuery("select 'Connected' from dual");
			resultset.next();
			String s = resultset.getString(1);
			System.out.println(s);
			statement.close();
			connection.close();
		} catch (Exception e) {
			System.out.println("The exception raised is:" + e);
		}
	}

}


Connect DB2
/**
 * ConnectDB2.java
 */
package com.javaworkspace.connectdb2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectDB2 {
	public static void main(String[] args) {
		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("com.ibm.db2.jcc.DB2Driver");
			connection = DriverManager.getConnection(
					"jdbc:db2://localhost:5021/EMPLOYEE", "username",
					"password");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect Postgre
/**
 * ConnectPostgre.java
 */
package com.javaworkspace.connectpostgre;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectPostgre {
	public static void main(String[] args) {
		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("org.postgresql.Driver");
			connection = DriverManager.getConnection(
					"jdbc:postgresql://localhost:5432/EMPLOYEE", "postgres",
					"password");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect ADS
/**
 * ConnectADS.java
 */
package com.javaworkspace.connectads;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectADS {
	public static void main(String[] args) {
		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;
		
		try {
			Class.forName("com.extendedsystems.jdbc.advantage.ADSDriver");
			connection = DriverManager.getConnection(
					"jdbc:extendedsystems:advantage://localhost:6262;catalog=C:\\yourDatbase\\data;TableType=cdx;LockType=proprietary", "AdsSys", "no");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect Derby
/**
 * ConnectDerby.java
 */
package com.javaworkspace.connectderby;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectDerby {
	public static void main(String[] args) {

		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			connection = DriverManager
					.getConnection("jdbc:derby:C:/DerbyDatabase/EMPLOYEE");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect Firebird
/*
 * ConnectFirebirdDB.java
 */
package com.javaworkspace.connectfirebirddb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectFirebirdDB {
	public static void main(String[] args) {

		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("org.firebirdsql.jdbc.FBDriver");
			connection = DriverManager
					.getConnection(
							"jdbc:firebirdsql://localhost:3050/C:/FirebirdDB/EMPLOYEEDB.fdb",
							"sysdba", "masterkey");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect H2
/*
 * ConnectH2DB.java
 */
package com.javaworkspace.connecth2db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectH2DB {
	public static void main(String[] args) {

		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("org.h2.Driver");
			connection = DriverManager.getConnection(
					"jdbc:h2:C:\\H2DB\\database\\EMPLOYEEDB", "sa", "");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect SQLite
/**
 * ConnectSQLite.java
 */
package com.javaworkspace.connectsqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectSQLite {
	public static void main(String[] args) {

		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("org.sqlite.JDBC");
			connection = DriverManager
					.getConnection("jdbc:sqlite:C:\\SQLite\\EMPLOYEE.db");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


Connect SmallSQL
/**
 * ConnectSmallSQL.java
 */
package com.javaworkspace.connectsmallsql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class ConnectSmallSQL {
	public static void main(String[] args) {

		Connection connection = null;
		ResultSet resultSet = null;
		Statement statement = null;

		try {
			Class.forName("smallsql.database.SSDriver");
			connection = DriverManager
					.getConnection("jdbc:smallsql:C:\\SmallSQL\\database\\EMPLOYEEDB");
			statement = connection.createStatement();
			resultSet = statement
					.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
			while (resultSet.next()) {
				System.out.println("EMPLOYEE NAME:"
						+ resultSet.getString("EMPNAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}


http://www.javaworkspace.com/donations.do
分享到:
评论

相关推荐

    ​DBeaver支持虚谷数据库连接安装包及插件

    DBeaver是一款基于Java开发的通用数据库管理工具和SQL客户端,支持MySQL、PostgreSQL、Oracle、DB2、MSSQL、Sybase、Mimer、HSQLDB、Derby以及其他兼容JDBC的数据库。DBeaver提供了图形界面用来查看数据库结构、执行...

    hibernate连接各种数据库的方法

    ### 使用Hibernate连接各种数据库的方法 #### 一、前言 Hibernate是Java开发中非常流行的ORM(对象关系映射)框架之一,它简化了数据库操作,让开发者能够更加专注于业务逻辑而不是复杂的SQL语句编写。本文将详细...

    数据库连接大全

    本文档将详细介绍如何使用Java来连接各种主流数据库,包括Oracle、SQL Server、DB2、Informix、Sybase、MySQL、PostgreSQL、SAPDB、InterBase以及HSQLDB。 #### 二、连接Oracle数据库 要连接Oracle数据库,首先...

    java 数据库驱动大全

    jar使用说明: 驱动 对应数据库 --------------------------------------...mysql-connector-java-5.1.6-bin.jar Mysql数据库 ---------------------------------------------------------- sybase.jar Sybase数据库

    hibernate中所有数据库方言

    Hibernate是一个开源的对象关系映射(ORM)框架,用于Java应用与各种关系型数据库进行交互。它提供了强大的功能,如对象持久化、查询语言支持、事务管理等,大大简化了数据访问层的开发工作。 ### Hibernate支持的...

    DBeaver22很好用

    MySQL、Oracle、PostgreSQL、IBM DB2、Microsoft SQL Server、clickhouse、Sybase、ODBC、Java DB (Derby)、Firebird (Interbase)、HSQLDB、SQLite、Mimer、H2、IBM Informix、SAP MAX DB、Cache、Ingres、Linter、...

    运用JAVA进行数据库应用开发培训

    常见的数据库产品有Oracle、SQL Server、DB2、Sybase、Informix、MySQL、hsqldb、InterBase等,以及更贴近个人用户级别的Foxpro、Access和Excel。 数据库对象是数据库中的基本元素,如表(table)、字段(column)...

    数据库基础

    - 关系型数据库(RDBMS):例如Oracle、MySQL、SQL Server、DB2、HSQLDB、Derby等。它们使用二维表结构存储数据,并通过关系代数进行操作。 - 对象型数据库(OODBMS):以对象作为数据存储的基本单位。 - NoSQL...

    DBeaver Enterprise 21.1企业版 (DBEE21.1+JDK11+Agent)

    MySQL、Oracle、PostgreSQL、IBM DB2、Microsoft SQL Server、clickhouse、Sybase、ODBC、Java DB (Derby)、Firebird (Interbase)、HSQLDB、SQLite、Mimer、H2、IBM Informix、SAP MAX DB、Cache、Ingres、Linter、...

    Java嵌入式数据库H2学习大全

    H2还兼容多种数据库系统,包括IBM DB2、Apache Derby、HSQLDB、MS SQL Server、MySQL、Oracle和PostgreSQL等,这使得迁移至H2时更容易上手。 #### 安全属性 H2在安全方面做了大量工作,包括但不限于: - 提供针对...

    dbeaver-setup.exe

    MySQL、Oracle、PostgreSQL、IBM DB2、Microsoft SQL Server、clickhouse、Sybase、ODBC、Java DB (Derby)、Firebird (Interbase)、HSQLDB、SQLite、Mimer、H2、IBM Informix、SAP MAX DB、Cache、Ingres、Linter、...

    dbeaver.zip

    MySQL、Oracle、PostgreSQL、IBM DB2、Microsoft SQL Server、clickhouse、Sybase、ODBC、Java DB (Derby)、Firebird (Interbase)、HSQLDB、SQLite、Mimer、H2、IBM Informix、SAP MAX DB、Cache、Ingres、Linter、...

    JDBC连接数据库大全

    1. **DriverManager**:用于加载驱动程序并建立数据库连接。 2. **Connection**:表示与数据库之间的连接。 3. **Statement**:用于发送SQL语句到数据库。 4. **ResultSet**:用于存储查询结果。 #### 二、JDBC驱动...

    一款强大的多数据连接工具dbeaver,页面操作简洁。

    MySQL、Oracle、PostgreSQL、IBM DB2、Microsoft SQL Server、clickhouse、Sybase、ODBC、Java DB (Derby)、Firebird (Interbase)、HSQLDB、SQLite、Mimer、H2、IBM Informix、SAP MAX DB、Cache、Ingres、Linter、...

    dbeaver-ee-21.2.0-win32.win32.x86_agent_64.zip

    MySQL、Oracle、PostgreSQL、IBM DB2、Microsoft SQL Server、clickhouse、Sybase、ODBC、Java DB (Derby)、Firebird (Interbase)、HSQLDB、SQLite、Mimer、H2、IBM Informix、SAP MAX DB、Cache、Ingres、Linter、...

    jdbc 驱动包全集

    JDBC 驱动包合集 JAR包: Access cache db2 derby firebird hsqldb informix ingres jtds maxdb mssql mysql oracle postgresql sqlite

    H2Database连接配置.pdf

    H2的兼容性非常强,支持多种数据库方言,包括与DB2、Apache Derby、HSQLDB、MSSQL Server、MySQL、Oracle以及PostgreSQL数据库的兼容性。它还具备了SQL注入问题的解决方案,使得应用更加安全。 H2数据库支持集群,...

    数据库连接工具dbeaver7.0.0版本

    数据库链接工具dbeaver-ce-7.0.0-x86_64-setup,兼容大部分数据库,使用简单,支持 MySQL, PostgreSQL, Oracle, DB2, MSSQL, Sybase, Mimer, HSQLDB, Derby, 以及其他兼容 JDBC 的数据库。 帮助你免除安装各种...

    DBeaver数据库连接工具

    DBeaver 是一个通用的数据库管理工具和 SQL 客户端,支持 MySQL, PostgreSQL, Oracle, DB2, MSSQL, Sybase, GaussDB, HSQLDB, Derby, 以及其他兼容 JDBC 的数据库。

Global site tag (gtag.js) - Google Analytics