几种JDBC的实现

package cn.itcast.jdbc.test;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.junit.Test;

import cn.itcast.jdbc.util.JdbcUtil;

import com.mysql.jdbc.Connection;

public class TestJDBC {
 
 
 @Test
 //使用driver接口的mysql实现类连接mysql数据库
 public void testOne(){
  try {
   java.sql.Driver driver = new com.mysql.jdbc.Driver();
   String url = "jdbc:mysql://localhost:3306/dbtest";
   Properties info = new Properties();
   info.setProperty("user", "root");
   info.setProperty("password", "root");
   Connection conn = (Connection) driver.connect(url, info);
   System.out.println(conn);
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
  
 @Test
 //采用forName
 public void testThree(){
  
  try {
   Class.forName("com.mysql.jdbc.Driver");
   String url = "jdbc:mysql://localhost:3306/dbtest";
   Connection conn = (Connection) DriverManager.getConnection(url, "root", "root");
   Statement state =  conn.createStatement();
   ResultSet rs = state.executeQuery("select * from users");
   while(rs.next()){
    String id = rs.getString(1);
    String name = rs.getString(2);
    String pwd = rs.getString(3);
    System.out.println(id + name + pwd);
    
   }
   
   System.out.println(conn.getAutoCommit());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 @Test
 //采用注册驱动的方式
 public void testFour(){
  
  try {
   String url = "jdbc:mysql://localhost:3306/dbtest";
   Properties info = new Properties();
   info.setProperty("user", "root");
   info.setProperty("password", "root");
   DriverManager.registerDriver(new com.mysql.jdbc.Driver());
   Connection conn = (Connection) DriverManager.getConnection(url, info);
   System.out.println(conn);
   Statement state = conn.createStatement();
   String sql = "insert into users(id,name,pwd) values('U123','mic','123456')";
   String sql2 = "delete from users where id='U123'";
   state.execute(sql2);
   state.close();
   conn.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值