java连接mysql需要接口吗_jdbcjava数据库连接各接口的介绍

1

Connection   与特定数据库的连接(会话)。在连接上下文中执行 SQL 语句并返回结果。   public interface Connection              extends           Wrapper           Connection.createStatement();返回值类型Statement     创建一个 Statement 对象来将 SQL 语句发送到数据库。          public interface ResultSet              extends           Wrapper    表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。

38909f5f541e565179dcd8aeb9e5365d.png

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 * */

2b87dc8fc667da02652cc86bc70b4dcb.png

3

public class DBUtil {   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"));

b78450ed0c41f4681350c82d8bf437c3.png

4

driver=properties.getProperty("driver");        url=properties.getProperty("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 block    e.printStackTrace();   } }

98fbc62850b9e1be00d097dd46b9259d.png

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("关闭连接失败");  } }}

4815e17909c6febaaa82c96a3cd6d344.png

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);  } }}

70a178977b14b26b6a2b23ecbe536dc1.png

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值