01_JDBC基本使用------随堂练习
练习题1:从控制台向数据库的表customers中插入一条数据,表结构如下:
练习题2:创立数据库表 examstudent,表结构如下:
向数据表中添加如下数据:
代码实现1:插入一个新的student 信息
请输入考生的详细信息
Type:
IDCard:
ExamCard:
StudentName:
Location:
Grade:
信息录入成功!
@Test
//获取连接的方法
public Connection getConnection() throws IOException, ClassNotFoundException, SQLException{
//加载配置文件
InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties pro = new Properties();
pro.load(in);
//定义四要素
String url = pro.getProperty("url");
String driver = pro.getProperty("driver");
String user = pro.getProperty("user");
String password = pro.getProperty("password");
//加载mysql的driver
Class.forName(driver);
//获取连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
@Test
public void testpstmtInsert() throws ClassNotFoundException, IOException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入考生的详细信息:");
System.out.println("Type:")