package connection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
//url="jdbc:mysql://localhost:3306/test" test指的是数据库
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class ConnectionText {
@Test
public void test() throws SQLException{
//方式一
Driver d = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.setProperty("user", "mysqlChen");
info.setProperty("password", "1234");
Connection conn = d.connect(url, info);
System.out.println(conn);
}
//方式二
@Test
public void test1() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.setProperty("user", "mysqlChen");
info.setProperty("password", "1234");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
//方式三:使用driverMannager 替换Driver
@Test
public void test3() throws Exception {
//1.获取Driver实现类的对象
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2.提供另外三个链接的基本信息
String url ="jdbc:mysql://localhost:3306/test";
String user = "mysqlChen";
String password="1234";
//注册驱动
DriverManager.registerDriver(driver);
//获取链接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//方式四:在3的基础上优化
@Test
public void test4() throws Exception {
//2.提供另外三个链接的基本信息
String url ="jdbc:mysql://localhost:3306/test";
String user = "mysqlChen";
String password="1234";
//2.获取Driver实现类的对象
Class.forName("com.mysql.jdbc.Driver");
// Driver driver = (Driver) clazz.newInstance();
// //注册驱动,相较于方式三可以省略如下的操作
//在mysql的Driver中声明了静态代码块自动帮我们注册驱动
// DriverManager.registerDriver(driver);
//3.获取链接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//方式五:将数据库需要的4哥基本信息生命在配置文件中,通过读取配置文件的方式,获取连接
/*
* 好处
* 1.实现了数据与代码的法分离,实现了解耦
* 2.如果需要修改配置文件信息,就可以避免程序重新打包。
* */
@Test
public void test5() throws Exception {
//1.读取配置文件中的4个基本信息
InputStream is = ConnectionText.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url =properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
}
}
创建数据库与java程序的五种方式,以及statement 的弊端
statment:
Scanner scan = new Scanner(System.in);
//
// System.out.print("用户名:");
// String userName = scan.nextLine();
// System.out.print("密 码:");
// String password = scan.nextLine();
//
// // SELECT user,password FROM user_table WHERE USER = '1' or ' AND PASSWORD = '
// // ='1' or '1' = '1';
// String sql = "SELECT user,password FROM user_table WHERE USER = '" + userName + "' AND PASSWORD = '" + password
// + "'";
// User user = get(sql, User.class);
// if (user != null) {
// System.out.println("登陆成功!");
// } else {
// System.out.println("用户名或密码错误!");
// }
user,password FROM user_table WHERE USER = '1' or ' AND PASSWORD = '
// // ='1' or '1' = '1'
这段代码粘贴到mysql中显示就是statment的弊端
总体来说理解方式五即可
|