JDBC接口(API)包括两个层次:
import org.junit.Test;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class DriverTest {
/**
* 获取mysql连接
*/
@Test
public void test() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
Driver driver = null;
//1.获取驱动
String driverClassName = "com.mysql.cj.jdbc.Driver";
Class clazz = Class.forName(driverClassName);
driver =(Driver) clazz.newInstance();
//2.获取连接
String url = "jdbc:mysql://10.10.128.100:3306/dw";
Properties props = new Properties();
props.setProperty("user","root");
props.setProperty("password","nio123456!A");
Connection conn = driver.connect(url,props);
System.out.println(conn);
conn.close();
}
}
public class DriverTest {
@Test
public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
//1.注册驱动
String driverClassName = "com.mysql.cj.jdbc.Driver";
Class clazz = Class.forName(driverClassName);
Driver driver = (Driver) clazz.newInstance();
DriverManager.registerDriver(driver);
//2.获取连接
String url = "jdbc:mysql://10.10.128.100:3306/dw";
String user = "root";
String password = "nio123456!A";
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
conn.close();
}
}
import org.junit.Test;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DriverTest {
@Test
public void test3() throws ClassNotFoundException, SQLException {
String driverClassName = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://10.10.128.100:3306/dw";
String user = "root";
String password = "nio123456!A";
//1.加载驱动
Class.forName(driverClassName);
//2.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
application.properties
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.10.128.100:3306/dw?useUnicode=true&characterEncoding=utf-8
username: root
password: nio123456!A
import org.junit.Test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DriverTest {
@Test
public void test4() throws IOException, ClassNotFoundException, SQLException {
Properties props = new Properties();
props.load(this.getClass().getClassLoader().getResourceAsStream("application.properties"));
String driverClassName = props.getProperty("driverClassName");
String url = props.getProperty("url");
String user = props.getProperty("username");
String password = props.getProperty("password");
Class.forName(driverClassName);
//2.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class jdbcUtils {
/**
* 获取连接
* @return
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
Properties props = new Properties();
props.load(JdbcUtils.class.getClassLoader().getResourceAsStream("application" +
".properties"));
String driverClassName = props.getProperty("driverClassName");
String url = props.getProperty("url");
String user = props.getProperty("username");
String password = props.getProperty("password");
Class.forName(driverClassName);
//2.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
}