日期:2014-05-16 浏览次数:20431 次
1、三方关系:规范制定者、规范实现者、规范使用者
?
?
规范制定者:sun JDBC
规范实现者:数据库厂商
应用程序开发者:程序员、软件工程师
?
?
2、JDBC实现的接口:
?
?
4、三种数据库连接的默认端口号
mysql:3306 ?sqlserver:1433 ?oracle:1521
连接的三种写法:
?
5、JDBC 连接MYSQ数据库
?
package com.metarnet.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class CopyOfTestJdbcForMySQL { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("驱动程序加载成功!"); //得到连接 conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db_eshop", "root", "root"); System.out.println("连接成功!"); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from admin"); while(rs.next()) { System.out.println(rs.getString(3)); } } catch (ClassNotFoundException e) { System.out.println("驱动程序加载失败"); } catch (SQLException e) { System.out.println("连接失败"); e.printStackTrace(); } finally { try { if(rs != null) { rs.close(); rs = null; } if(stmt != null) { stmt.close(); stmt = null; } if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }
?
?
?6、JDBC连接SQLServer数据库
?
package com.metarnet.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestJdbcForSqlServer { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //加载驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("驱动程序加载成功!"); //得到连接 conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=db_eshop", "sa", "1234"); System.out.println("连接成功!"); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from admin"); while(rs.next()) { System.out.println(rs.getString(3)); } } catch (ClassNotFoundException e) { System.out.println("驱动程序加载失败"); } catch (SQLException e) { System.out.println("连接失败"); e.printStackTrace(); } finally { try { if(rs != null) { rs.close(); rs = null; } if(stmt != null) { stmt.close(); stmt = null; } if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }?
?
7、JDBC连接oracle数据库
?
package com.metarnet.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestJdbcForMySQLOracle { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("驱动程序加载成功!"); //得到连接 conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:db_eshop", "r