1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| package org.westos.pojo; import org.jboss.arquillian.test.spi.annotation.TestScoped; import org.junit.Test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Scanner; public class sqlScentence { @Test public void query() throws Exception { Connection connection = null; PreparedStatement preparedStatement = null; ArrayList<Object> objects = new ArrayList<>(); ResultSet resultSet = null; String StudentNo = null; String password = null; String sql = null; Scanner scanner = new Scanner(System.in); System.out.println("请输入你要查询的学号"); StudentNo = scanner.nextLine(); System.out.println("请输入你的密码"); password = scanner.nextLine(); connection = JDBCUtils.getConnection(); sql = "select * from student where StudentNo=?and LoginPwd =?"; objects.add(StudentNo); objects.add(password); Object[] array = objects.toArray(); ResultSet query = JDBCUtils.query(connection, preparedStatement, sql, resultSet, array); while (query.next()){ System.out.println(query.getObject(1)); System.out.println(query.getObject(2)); System.out.println(query.getObject(3)); } } @Test public void insert ()throws Exception{ Connection connection = null; PreparedStatement preparedStatement = null; ArrayList<Object> objects = new ArrayList<>(); connection = JDBCUtils.getConnection(); objects.add(1018); objects.add(1234567); objects.add("赵文君"); objects.add(2); objects.add(4); objects.add("28282828282"); objects.add("重庆"); objects.add("1986-12-11 00:00:00"); objects.add("[email protected]"); objects.add("450323198612314232"); Object[] array = objects.toArray(); String sql = "insert into student (StudentNo,LoginPwd,StudentName,Sex,GradeId,Phone,Address,BornDate,Email,IdentityCard)values(?,?,?,?,?,?,?,?,?,?)"; int update = JDBCUtils.update(connection, preparedStatement, sql, array); if (update>0){ System.out.println(666666666); } } @Test public void update()throws Exception{ Connection connection = null; PreparedStatement preparedStatement = null; ArrayList<Object> objects = new ArrayList<>(); objects.add(1000); objects.add(2); Object[] array = objects.toArray(); connection = JDBCUtils.getConnection(); String sql = "update result set StudentResult =100 where StudentNo = ? and SubjectNo= ? "; int update = JDBCUtils.update(connection, preparedStatement, sql, array); if (update>0){ System.out.println(666666); } } @Test public void delete()throws Exception{ Connection connection = null; PreparedStatement preparedStatement = null; ArrayList<Object> objects = new ArrayList<>(); connection = JDBCUtils.getConnection(); objects.add(1000); Object[] array = objects.toArray(); String sql = "delete from student where StudentNo=?"; int i = JDBCUtils.update(connection, preparedStatement, sql, array); if (i>0){ System.out.println(67899); } } }
|