赞
踩
JDBC :sun公司提供的一套api(java.sql,javax.sql),
使用这套api可以实现对数据的连接和操作(DML、DDL)
如下代码来测试:获取MySQL数据库连接
要想获取连接,准备工作:
①MySQL服务开启
②连接的基本条件:指明数据库的IP地址、端口号、用户名、密码
③导入MySQL的驱动(即为jdbc中的相关接口的实现类和集合)
3.网络编程中的url:代表着互联网中的某一资源的地址。
http://192.168.21.38:8080/zhoushun/index.jsp?user=tom
协议 IP地址 端口号 目标资源 参数列表
1.提供MySQL中的Driver接口的实现类
Driver driver = new com.mysql.jdbc.Driver();
2.注册驱动
DriverManager.registerDriver(driver);
3.获取连接的基本信息
String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
String user = "root";
String password = "root";
4.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
@Test public void test1() throws SQLException { //提供MySQL中的Driver接口的实现类 Driver driver = new com.mysql.jdbc.Driver(); //注册驱动 DriverManager.registerDriver(driver); String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名 String user = "root"; String password = "root"; //获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
1.提供MySQL中的Driver接口的实现类
String className = "com.mysql.jdbc.Driver";
Class cla = Class.forName(className);
Driver driver = (Driver) cla.newInstance();
2.注册驱动
DriverManager.registerDriver(driver);
3.获取连接的基本信息
String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
String user = "root";
String password = "root";
4.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
@Test public void test2() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException { //提供MySQL中的Driver接口的实现类 String className = "com.mysql.jdbc.Driver"; Class cla = Class.forName(className); Driver driver = (Driver) cla.newInstance(); //注册驱动 DriverManager.registerDriver(driver); String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名 String user = "root"; String password = "root"; //获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
1.获取连接的基本信息
String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
String user = "root";
String password = "root";
2.加载驱动
Class.forName(className);
3.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
@Test public void test3() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException { //1.获取连接的4个基本信息 String className = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名 String user = "root"; String password = "root"; //2.加载驱动 Class.forName(className); //获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }
配置文件在src目录下新建一个jdbc.properties文件
在文件中写入如下代码:
className=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root
1.读取配置文件中的4个基本信息
Properties pros = new Properties();
//加载资源的路径默认在src下
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
pros.load(is);
String className = pros.getProperty("className");
String url = pros.getProperty("url");
String user = pros.getProperty("user");
String password = pros.getProperty("password");
2.加载驱动
Class.forName(className);
3.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
@Test public void test4() throws Exception { //读取配置文件中的4个基本信息 Properties pros = new Properties(); //加载资源的路径默认在src下 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); pros.load(is); String className = pros.getProperty("className"); String url = pros.getProperty("url"); String user = pros.getProperty("user"); String password = pros.getProperty("password"); //2.加载驱动 Class.forName(className); //3,获取连接 Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); }
目录:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。