package JDBCTest;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.stream.Stream;
import org.junit.Test;
public class Test01 {
@Test
public void test() throws SQLException {
Driver driver =new com.mysql.jdbc.Driver();
String url ="jdbc:mysql://localhost:3306/cc?jdbcstudy?useUnicode=true&characterEncoding=utf-8&useSSL=true";
Properties info =new Properties();
info.setProperty("user","root");
info.setProperty("password","root");
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
@Test
public void test2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver =(Driver)clazz.getConstructor().newInstance();
String url ="jdbc:mysql://localhost:3306/cc";
Properties info =new Properties();
info.setProperty("user","root");
info.setProperty("password","root");
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
@Test
public void test3() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
String url ="jdbc:mysql://192.168.31.101:3306";
String user ="root";
String password ="root";
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver =(Driver)clazz.getConstructor().newInstance();
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
@Test
public void test4() throws ClassNotFoundException, SQLException {
String url ="jdbc:mysql://localhost:3306";
String user ="root";
String password ="root";
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
@Test
public void test5() throws IOException, SQLException {
Class test01Class = Test01.class;
ClassLoader classLoader = test01Class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
Connection connection = DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("user"), properties.getProperty("password"));
System.out.println(connection);
}
}
package JDBCTest;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.stream.Stream;
public class LoaderTest {
public static void main(String[] args) throws IOException {
Class loaderTestClass = LoaderTest.class;
ClassLoader classLoader = loaderTestClass.getClassLoader();
System.out.println(classLoader);
InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
System.out.println(properties.getProperty("user")+" "+properties.getProperty("password"));
}
}
user=root
password=root
url=jdbc:mysql://localhost:3306/cc
ClassDriver=com.mysql.jdbc.Driver
|