大家好,又见面了,我是你们的朋友全栈君。
数据的持久化:把数据保存到磁盘上。 JDBC是java访问数据库的基石,JDO,Hibernate,Mybatis等都是基于JDBC JDBC是一个独立于特定数据库的管理系统,通用的SQL数据库存取和操作的公共接口
配置文件:jdbc.properties
user=root
password=abc123
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver
public static void main(String[] args) throws Exception {
//方式一
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost1:3306/test";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","123456");
Connection connect = driver.connect(url, info);
//方式二:反射实现获取驱动对象 不用出现第三方的类
Class clazz = Class.forName("com.mysql.jdbc.Driver");
//权限要够
Driver driver1 = (Driver) clazz.newInstance();
//方式三:使用DriverManager替换Driver
//获取驱动实现类对象
Class clazz1 = Class.forName("com.mysql.jdbc.Driver");
Driver driver2 = (Driver) clazz1.newInstance();
String url1 = "jdbc:mysql://localhost1:3306/test";
String user1 = "root";
String password1 = "123456";
//注册驱动
DriverManager.registerDriver(driver2);
//获取连接
Connection connection = DriverManager.getConnection(url, user1, password1);
System.out.println(connection);
//方式4:优化方式四
Class.forName("com.mysql.jdbc.Driver");
//注册驱动
DriverManager.registerDriver(driver2);
//获取连接
Connection connection1 = DriverManager.getConnection(url, user1, password1);
System.out.println(connection1);
//方式5:读取配置文件
InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url2 = properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
Class.forName(driverClass);
Connection connection2 = DriverManager.getConnection(url, user, password);
}
PrepareStatement相比Statement能够解决SQL注入,拼串,能操作Blob数据,实现更高效的批量操作,
添加
public static void main(String[] args) throws Exception{
InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(url, user, password);
//添加数据
//预编译sql语句,PreparedStatement实例
String sql = "insert into customers(name,email,birth) values(?,?,?)";
java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
//填充占位符
preparedStatement.setString(1,"哪吒");
preparedStatement.setString(2,"nezha@gmail.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("1000-01-01");
preparedStatement.setDate(3, (java.sql.Date) new Date(date.getTime()));
//执行sql
preparedStatement.execute();
preparedStatement.close();
connection.close();
}
修改
//预编译sql语句,PreparedStatement实例
String sql = "update customers set name = ? where id = ?";
java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
//填充占位符
preparedStatement.setString(1,"莫扎特");
preparedStatement.setObject(2,28);
//执行sql
preparedStatement.execute();
preparedStatement.close();
connection.close();
查找
//预编译sql语句,PreparedStatement实例
String sql = "select id,name,email,birth from customers where id = ?";
java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
//填充占位符
//执行sql
ResultSet resultSet = preparedStatement.executeQuery();
//获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = preparedStatement.getMetaData();
int columnCount = rsmd.getColumnCount();
while (resultSet.next()){
Customer cust = new Customer();
for(int i = 0;i < columnCount;i ++){
Object object = resultSet.getObject(i + 1);
//获取每个列的列明
String columnName = rsmd.getColumnName(i + 1);
Field declaredField = Customer.class.getDeclaredField(columnName);
declaredField.setAccessible(true);
declaredField.set(cust,object);
}
}
preparedStatement.close();
connection.close();
resultSet.close();
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168823.html原文链接:https://javaforall.cn