JDBC第一个程序——5种不同连接方式

JDBC程序编写步骤

  1. 注册驱动——加载Driver类

  2. 获取连接——得到Conneciton

  3. 执行增删改查——发送SQL给mysql执行

  4. 释放资源——关闭相关的连接

数据库连接方式1

package godairo.jdbc.myjdbc;


import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 这是第一个JDBC程序,完成简单的操作。
 */
public class Jdbc01 {
   public static void main(String[] args) throws SQLException {
	  //1.注册驱动
	  Driver driver = new Driver();//创建driver对象
	  //2.得到连接
	  //(1)	jdbc:mysql://   规定好的,表示协议,通过jdbc的方式连接mysql
	  //(2)	localhost 		主机,也可以是一个ip地址
	  //(3) 3306			表示mysql监听的端口,比如说mysql那边变为3307,我们也要变成3307
	  //(4) jdbcstudy		表示连接到mysql的哪个数据库
	  //(5) mysql的连接本质就是Socket连接
	  String url = "jdbc:mysql://localhost:3306/jdbcstudy";//代表要连接到哪个数据库
	  //将用户和密码放入到Properties对象中
	  Properties properties = new Properties();
	  //说明:user和password是规定好的,后面的值根据实际情况写
	  properties.setProperty("user","root");//用户
	  properties.setProperty("password","root");//密码
	  //这个connect就可以理解成一个网络连接
	  Connection connect = driver.connect(url, properties);

	  //3.执行SQL语句
	  String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110')";
	  String sqlupdate = "update actor set name='周星驰' where id=1";
	  String sqlDelete = "delete from actor where id =1";
	  //通过Statement对象来发送sql语句,并由mysql执行,并返回执行结果
	  Statement statement = connect.createStatement();
	  int i = statement.executeUpdate(sql);//如果是dml语句,返回的就是影响行数,如果生效就是返回1,失败就是返回0.
	  System.out.println(i>0?"成功":"失败");
	  //4.关闭连接资源
	  statement.close();
	  connect.close();
   }
}

数据库连接方式2

方式1会直接使用com.mysql.jdbc.Driver(),属于静态加载,灵活性差,依赖强

//方式2
   @Test
   public void connect02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
	  //使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
	  Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
	  Driver driver = (Driver) aClass.newInstance();
	  String url = "jdbc:mysql://localhost:3306/jdbcstudy";//代表要连接到哪个数据库
	  //将用户和密码放入到Properties对象中
	  Properties properties = new Properties();
	  //说明:user和password是规定好的,后面的值根据实际情况写
	  properties.setProperty("user","root");//用户
	  properties.setProperty("password","root");//密码
	  Connection connect = driver.connect(url, properties);
	  System.out.println("方式2:"+connect);
   }

数据库连接方式3

//方式3:使用DriverManager替代Driver进行统一管理
   @Test
   public void connect03() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
      //使用反射加载Driver
	  Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
	  Driver driver = (Driver)aClass.newInstance();
	  String url = "jdbc:mysql://localhost:3306/jdbcstudy";
	  String user = "root";
	  String password = "root";
	  DriverManager.registerDriver(driver);//注册Driver驱动
	  Connection connection = DriverManager.getConnection(url, user, password);
	  System.out.println("第三种方式="+connection);
   }

第三种连接相对而言,我们用DriverManager进行统一管理,具有更好的扩展性,看起来也比较清晰,可以把user和password直接创建出来,不用必须去创建Properties对象。

数据库连接方式4

//方式4:使用class.forName自动完成注册驱动,简化我们的代码
   //这种方式获取连接是使用的最多,推荐使用。
   @Test
   public void connect04() throws ClassNotFoundException, SQLException {
      //使用反射加载了Driver
	  
	  /**
	   * 源码:1.静态代码块,在类加载时,会执行一次
	   * 	  2.DriverManager.registerDriver(new Driver());
	   * 	  3.因此注册Driver的工作已经完成。
	   * static {
	   *       try {
	   *          DriverManager.registerDriver(new Driver());
	   *       } catch (SQLException var1) {
	   *          throw new RuntimeException("Can't register driver!");
	   *       }
	   *    }
	   */

	  //在加载Driver类时,完成注册
	  Class.forName("com.mysql.jdbc.Driver");
	  String url = "jdbc:mysql://localhost:3306/jdbcstudy";
	  String user = "root";
	  String password = "root";
	  Connection connection = DriverManager.getConnection(url, user, password);
	  System.out.println("第四种方式:"+connection);
   }

 有人会发现,不写Class.forName("com.mysql.jdbc.Driver");这段代码,也会连接成功。

原因:

1.在mysql驱动5.1.6可以不写Class.forName("com.mysql.jdbc.Driver");
2.从jdk1.5以后使用了JDBC4,不再需要显示调用class.forName()注册驱动,而是自动调用驱动jar包下META-INF/services/java.sql.Driver文本中的类名称去注册
3.但是还是建议要写上,因为更加明确。

数据库连接方式5

//方式5:在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
   @Test
   public void connect05() throws IOException, ClassNotFoundException, SQLException {
      //通过Properties对象获取配置文件信息
	  Properties properties = new Properties();
	  properties.load(new FileInputStream("src\\main\\resources\\jdbc.properties"));
	  String user = properties.getProperty("user");
	  String password = properties.getProperty("password");
	  String driver = properties.getProperty("driver");
	  String url = properties.getProperty("url");
	  Class.forName(driver);
	  Connection connection = DriverManager.getConnection(url, user, password);
	  System.out.println("方式5:"+connection);
   }

 一般都是用第五种方式,代码比较简洁


对数据进行添加、修改、删除

package godairo.jdbc.myjdbc;


import com.mysql.jdbc.Driver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 这是第一个JDBC程序,完成简单的操作。
 */
public class Jdbc01 {
   public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {


      //创建Properties对象然后下面准备获取数据用户名密码url等
	  Properties properties = new Properties();
	  properties.load(new FileInputStream("F:\\JavaWeb-Study\\JavaWeb\\JdbcStudy\\src\\main\\resources\\jdbc.properties"));
	  String user = properties.getProperty("user");
	  String password = properties.getProperty("password");
	  String driver = properties.getProperty("driver");
	  String url = properties.getProperty("url");
	  //加载Driver类完成注册
	  //得到连接
	  //(1)	jdbc:mysql://   规定好的,表示协议,通过jdbc的方式连接mysql
	  //(2)	localhost 		主机,也可以是一个ip地址
	  //(3) 3306			表示mysql监听的端口,比如说mysql那边变为3307,我们也要变成3307
	  //(4) jdbcstudy		表示连接到mysql的哪个数据库
	  //(5) mysql的连接本质就是Socket连接
	  Class.forName(driver);
	  //这个connect就可以理解成一个网络连接
	  Connection connection = DriverManager.getConnection(url, user, password);
      
	  
	  //3.执行SQL语句
	  String sql = "insert into actor values(null,'无名氏','男','1970-11-11','110')";
	  String sqlupdate = "update actor set name='周星驰' where id=1";
	  String sqlDelete = "delete from actor where id =1";
	  //通过Statement对象来发送sql语句,并由mysql执行,并返回执行结果
	  Statement statement = connection.createStatement();
	  int i = statement.executeUpdate(sql);//如果是dml语句,返回的就是影响行数,如果生效就是返回1,失败就是返回0.
	  System.out.println(i>0?"成功":"失败");
	  //4.关闭连接资源
	  statement.close();
	  connection.close();
	  
   }
}

在执行SQL语句之前,需要创建一个Statement对象,这个对象可以理解为就是用来发送sql语句的,并且由mysql执行,然后返回执行结果。增加、修改、删除只要用executeUpdate这个方法就行了。用完了需要关闭资源,Statement和connection都需要关闭。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GodAiro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值