package com.basicSql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * jdbc 链接Database执行 executeQuery() * * @author xrhou * */ public class TestJdbc { public static void main(String[] args) { dbconnMysql(); //dbconnInformix(); } /** * 链接Informix 数据库 */ public static void dbconnInformix(){ try { // 1 注册驱动,加载驱动,使用反射 Class.forName("com.informix.jdbc.IfxDriver").newInstance(); // 2 编写数据库连接字符串 String url="jdbc:informix-sqli://192.168.2.166:9088/bhrs:informixserver=online;NEWLOACLE=en_us,zh_cn,zh_tw;NEWCODESET=GB2312-80,8859-1,819,Big5 dbvisual"; String user="informix"; String password="informix"; Connection conn=DriverManager.getConnection(url, user, password); //3 设置连接状态 Statement stmt=conn.createStatement(); //4 执行查询语句 String sql="select * from dgwbw ;"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()){ String deptid=rs.getString("entryid"); String hm=rs.getString("hm"); System.out.println(deptid+","+hm); } //5 关闭资源连接 rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
/**
* 链接Mysql数据库 */ public static void dbconnMysql() { Connection conn =null; Statement stmt=null; ResultSet rs=null; try { // 1 注册驱动,加载驱动,使用反射 Class.forName("com.mysql.jdbc.Driver"); // new com.mysql.jdbc.Driver(); //相当与使用new 关键字 创建的一个实例 String url = "jdbc:mysql://127.0.0.1:3306/test"; String dbuser = "root"; String dbpwd = "root"; conn = DriverManager.getConnection(url, dbuser, dbpwd); stmt = conn.createStatement(); String sql = "select id,name from person"; rs = stmt.executeQuery(sql); { while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("name"); System.out.println(id + "," + name); } } } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex2) { ex2.printStackTrace(); }finally{ //5 关闭资源连接 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
/** * 链接Oracle数据库 */ public static void dbconnOracle() { try { // 1 注册驱动,加载驱动,使用反射 Class.forName("oracle.jdbc.driver.OracleDriver"); // new com.mysql.jdbc.Driver(); //相当与使用new 关键字 创建的一个实例 String url = "jdbc:oracle:thin:@127.0.0.1:1521:mydatabase"; String dbuser = "root"; String dbpwd = "root"; Connection conn = DriverManager.getConnection(url, dbuser, dbpwd); Statement stmt = conn.createStatement(); String sql = "select id,name from person"; ResultSet rs = stmt.executeQuery(sql); { while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("name"); System.out.println(id + "," + name); } } // 5 关闭资源 rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex2) { ex2.printStackTrace(); } } }