ConnectHSQLDB.java
Java代码
- /**
- * ConnectHSQLDB.java
- */
- package com.javaworkspace.connecthsqldb;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- */
- 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
Java代码
- /**
- * ConnectMySQL.java
- */
- package com.javaworkspace.connectmysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectMSSQL.java
- */
- package com.javaworkspace.connectmssql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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();
- }
- }
- }
Java代码
- // 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 http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectOracle.java
- */
- package com.javaworkspace.connectoracle;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectDB2.java
- */
- package com.javaworkspace.connectdb2;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectPostgre.java
- */
- package com.javaworkspace.connectpostgre;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectADS.java
- */
- package com.javaworkspace.connectads;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectDerby.java
- */
- package com.javaworkspace.connectderby;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /*
- * ConnectFirebirdDB.java
- */
- package com.javaworkspace.connectfirebirddb;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /*
- * ConnectH2DB.java
- */
- package com.javaworkspace.connecth2db;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectSQLite.java
- */
- package com.javaworkspace.connectsqlite;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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
Java代码
- /**
- * ConnectSmallSQL.java
- */
- package com.javaworkspace.connectsmallsql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author http://blog.csdn.net/w_basketboy
- *
- */
- 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();
- }
- }
- }
- }
JDBC连接各种数据库
最新推荐文章于 2018-11-14 16:05:00 发布