黑猴子的家:JDBC -> PreparedStateme
2019-03-01 本文已影响4人
黑猴子的家
1、创建 examstudent 表
DROP TABLE IF EXISTS `examstudent`;
CREATE TABLE `examstudent` (
`FlowID` int(11) NOT NULL AUTO_INCREMENT,
`Type` int(11) DEFAULT NULL,
`IDCard` varchar(20) DEFAULT NULL,
`ExamCard` varchar(20) DEFAULT NULL,
`StudentName` varchar(20) DEFAULT NULL,
`Location` varchar(20) DEFAULT NULL,
`Grade` int(11) DEFAULT NULL,
PRIMARY KEY (`FlowID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、插入如下数据
FlowID | Type | IDCard | ExamCard | StudentName | Location | Grade |
---|---|---|---|---|---|---|
1 | 4 | 412824195263214584 | 200523164754000 | 张峰 | 郑州 | 85 |
2 | 4 | 412824195263214585 | 200523164754001 | 孙明 | 大连 | 66 |
3 | 6 | 412824195263214586 | 200523164754002 | 刘能 | 沈阳 | 56 |
4 | 6 | 412824195263214587 | 200523164754003 | 赵虎 | 哈尔滨 | 95 |
5 | 4 | 412824195263214588 | 200523164754004 | 杨丽 | 北京 | 64 |
6 | 4 | 412824195263214589 | 200523164754005 | 王小红 | 太原 | 77 |
3、插入一行数据
INSERT INTO `examstudent` VALUES (
'1', '4', '412824195263214584', '200523164754000', '张峰', '郑州', '85');
4、插入多行数据的两种方式
方式一
INSERT INTO examstudent (
FlowID,Type,IDCard,ExamCard,StudentName,Location,Grade)
VALUES
('1', '4', '412824195263214584', '200523164754000', '张峰', '郑州', '85'),
('2', '4', '412824195263214585', '200523164754000', '刘能', '大连', '56');
方式二
INSERT INTO examstudent (FlowID,Type,IDCard,ExamCard,StudentName,Location,Grade)
SELECT '1', '4', '412824195263214584', '200523164754000', '张峰', '郑州', '84' UNION
SELECT '2', '5', '412824195263214585', '200523164754001', '刘能', '大连', '67' UNION
SELECT '3', '6', '412824195263214586', '200523164754002', '谢大脚', '哈尔滨', '48'
5、code
删改和增加数据的方式一样,就不在赘述
package com.yinggu.demo3;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;
import org.junit.Test;
import com.yinggu.utils.JDBCUtils;
* 使用PreparedStatement实现增删改查
* @author:黑猴子的家
* @博客 :https://www.jianshu.com/u/37fd8e2dff4c
public class TestPreparedStatement {
Scanner input = new Scanner(System.in);
@Test
public void testInsert() {
System.out.println("请输入考生的详细信息");
System.out.print("Type:");
int type = input.nextInt();
System.out.print("IDCard:");
String idCard = input.next();
System.out.print("ExamCard:");
String examCard = input.next();
System.out.print("StudentName:");
String studentName = input.next();
System.out.print("location:");
String location = input.next();
System.out.print("Grade:");
int grade = input.nextInt();
Connection connection = null;
PreparedStatement statement = null;
// ------------------------以下为连接数据库的步骤-------------------
try {
// 1.获取连接
connection = JDBCUtils.getConnection();
// 2.访问数据库,执行操作
statement = connection.prepareStatement(
"insert into examstudent values(null,?,?,?,?,?,?)");
statement.setInt(1, type);
statement.setString(2, idCard);
statement.setString(3, examCard);
statement.setString(4, studentName);
statement.setString(5, location);
statement.setInt(6, grade);
int update = statement.executeUpdate();
System.out.println(update > 0 ? "success" : "failure");
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
JDBCUtils.closeConnection(null, statement, connection);
}catch(Exception e){
e.printStackTrace();
}
}
}
}