jdbcjava数据库连接各接口的介绍
1、Connection与特定数据库的连接(会话)。在连接上下文中执行 SQL 语句并返回结果。public interface Connection extends WrapperConnection.createStatement();返回值类型Statement 创建一个 Statement 对象来将 SQL 语句发送到数据库。 public interface ResultSet extends Wrapper表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
2、package com.auto.java;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;/*** 通过配置文件读取相关连接信息*@Title *@Description TODO*@param *@return Demo5*@author chenchao*@Date 2017-9-7下午3:52:07**/
3、public class DBUt坡纠课柩il { private static Properties properties租涫疼迟=new Properties(); private static String driver=null; private static String url=null; private static String username=null; private static String password=null; //通过static静态代码块读取配置文件中的信息static{try {properties.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
4、 driver=properties.getProperty("driver"); url=properties.get霸烹钟爷Property("url"); username=properties.getProperty("username"); password=properties.getProperty("password"); //加载驱动 try {Class.forName(driver);} catch (ClassNotFoundException e) {System.out.println("加载驱动失败");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
5、//建立连接方法public static Connection getConnection() throws SQLException{return DriverManager.getConnection(url,username,password);} //关闭连接的方法public static void closeConnection(Connection con){try {con.close();} catch (SQLException e) {System.out.println("关闭连接失败");}}}
6、package com.auto.java;import java.sql.Connection;import java.sql.SQLException;/*** 测试通过配置文件连接数据库*/public class TestDBUtil {public static void main(String[] args) {Connection con = null;try {con = DBUtil.getConnection();} catch (SQLException e) {System.out.println("连接失败");} finally {DBUtil.closeConnection(con);}}}