一:上码
1.有的已经实现的就注释了
2.配置文件信息
package com.wyjedu.jdbc.PreparedStatement;import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;/*** 1.创建 admin表* 2.使用 PreparedStatement添加5条数据* 3.修改tom的记录,将name改成king* 4.删除一条的记录* 5.查询全部记录,并显示在控制台*/public class preparedStatement03 {public static void main(String[] args) throws Exception{text01();}//dml和create 操作public static void text01() throws Exception{//(1):获取配置文件信息Properties properties = new Properties();properties.load(new FileInputStream("src//mysql.properitys"));String url = properties.getProperty("url");String user = properties.getProperty("user");String password = properties.getProperty("password");String driver = properties.getProperty("driver");//(2):注册驱动Class.forName(driver);//(3):获取连接Connection connection = DriverManager.getConnection(url, user, password);//(4):组织sql,获取preparedStatement 接口// String sql = "create table admin(id int,name varchar(23))";// String sql = "insert into admin values (?,?)";//String sql = "update admin set name = ? where name = ?";String sql = "select * from admin";PreparedStatement preparedStatement = connection.prepareStatement(sql);//(5):执行语句Scanner scanner = new Scanner(System.in);// for (int i = 0; i < 5; i++) {
// int id = scanner.nextInt();
// String name = scanner.nextLine();
//
// preparedStatement.setInt(1,id);
// preparedStatement.setString(2,name);
//
// int p = preparedStatement.executeUpdate();
// // System.out.println(i);
// }/// 修改记录// preparedStatement.setString(1,scanner.nextLine());
// preparedStatement.setString(2,scanner.nextLine());
//
// int i = preparedStatement.executeUpdate();
// System.out.println(i);// 查询表的记录ResultSet resultSet = preparedStatement.executeQuery();while(resultSet.next()){int id = resultSet.getInt(1);//获取该行第一列String name = resultSet.getString(2);//获取该行的第二列System.out.println(id+" "+name);}//(6):关闭资源resultSet.close();preparedStatement.close();connection.close();}
}