java 中jdbc 链接数据库
2016-03-31 16:57
351 查看
package gz.itcast.a_jdbc;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
import org.junit.Test;
/**
* jdbc连接数据库
* @author APPle
*
*/
public class Demo1 {
//连接数据库的URL
private String url = "jdbc:mysql://localhost:3306/day17";
// jdbc协议:数据库子协议:主机:端口/连接的数据库 //
private String user = "root";//用户名
private String password = "root";//密码
/**
* 第一种方法
* @throws Exception
*/
@Test
public void test1() throws Exception{
//1.创建驱动程序类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本
//设置用户名和密码
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
//2.连接数据库,返回连接对象
Connection conn = driver.connect(url, props);
System.out.println(conn);
}
/**
* 使用驱动管理器类连接数据库(注册了两次,没必要)
* @throws Exception
*/
@Test
public void test2() throws Exception{
Driver driver = new com.mysql.jdbc.Driver();
//Driver driver2 = new com.oracle.jdbc.Driver();
//1.注册驱动程序(可以注册多个驱动程序)
DriverManager.registerDriver(driver);
//DriverManager.registerDriver(driver2);
//2.连接到具体的数据库
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
/**
* (推荐使用这种方式连接数据库)
* 推荐使用加载驱动程序类 来 注册驱动程序
* @throws Exception
*/
@Test
public void test3() throws Exception{
//Driver driver = new com.mysql.jdbc.Driver();
//通过得到字节码对象的方式加载静态代码块,从而注册驱动程序
Class.forName("com.mysql.jdbc.Driver");
//Driver driver2 = new com.oracle.jdbc.Driver();
//1.注册驱动程序(可以注册多个驱动程序)
//DriverManager.registerDriver(driver);
//DriverManager.registerDriver(driver2);
//2.连接到具体的数据库
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
import org.junit.Test;
/**
* jdbc连接数据库
* @author APPle
*
*/
public class Demo1 {
//连接数据库的URL
private String url = "jdbc:mysql://localhost:3306/day17";
// jdbc协议:数据库子协议:主机:端口/连接的数据库 //
private String user = "root";//用户名
private String password = "root";//密码
/**
* 第一种方法
* @throws Exception
*/
@Test
public void test1() throws Exception{
//1.创建驱动程序类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本
//设置用户名和密码
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
//2.连接数据库,返回连接对象
Connection conn = driver.connect(url, props);
System.out.println(conn);
}
/**
* 使用驱动管理器类连接数据库(注册了两次,没必要)
* @throws Exception
*/
@Test
public void test2() throws Exception{
Driver driver = new com.mysql.jdbc.Driver();
//Driver driver2 = new com.oracle.jdbc.Driver();
//1.注册驱动程序(可以注册多个驱动程序)
DriverManager.registerDriver(driver);
//DriverManager.registerDriver(driver2);
//2.连接到具体的数据库
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
/**
* (推荐使用这种方式连接数据库)
* 推荐使用加载驱动程序类 来 注册驱动程序
* @throws Exception
*/
@Test
public void test3() throws Exception{
//Driver driver = new com.mysql.jdbc.Driver();
//通过得到字节码对象的方式加载静态代码块,从而注册驱动程序
Class.forName("com.mysql.jdbc.Driver");
//Driver driver2 = new com.oracle.jdbc.Driver();
//1.注册驱动程序(可以注册多个驱动程序)
//DriverManager.registerDriver(driver);
//DriverManager.registerDriver(driver2);
//2.连接到具体的数据库
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
相关文章推荐
- ZeroMQ 简单的高性能REP-REQ模型,Java语言版本
- eclipse报错multiple annotations found at this line
- java套打pdf做模版itext填数据
- SpringMVC与Struts2的对比
- SrpingMvc_mybatis_maven搭建web项目
- 6.spring AOP
- Java并发之(2):线程通信wait/notify(TIJ_21_5)
- Java包装类
- maven安装以及eclipse配置maven
- RxJava简单应用 基础篇
- Spring4与Quartz整合时找不到spring-context jar包中quoart包
- synchronized
- 建立一个Hello World级别的Spring项目
- 【Java】 JTextField文本框实时监控
- 常用DOS命令
- Spring Bean 创建过程
- Java解决火狐浏览器使用uploadify上传报错302以及报IO error错问题
- Java中的wait()方法与notify(),notifyAll()方法
- Web必知必会Cookie与Session
- Java多线程模拟实现消费者生产者问题