Java Code Examples for com.mysql.jdbc.PreparedStatement#executeQuery()
The following examples show how to use
com.mysql.jdbc.PreparedStatement#executeQuery() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CourseDao.java From StudentManagement with MIT License | 6 votes |
public ArrayList<Course> query_all_course() { Connection conn = DBUtils.getConnection(); String sql = "select * from course order by cno;"; ArrayList<Course> results = new ArrayList<Course>(); try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { Course temp = new Course(); temp.setCno(rs.getString("Cno")); temp.setCname(rs.getString("Cname")); temp.setCteacher(rs.getString("Cteacher")); temp.setCcredit(rs.getInt("Ccredit")); results.add(temp); } // 关闭资源 rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtils.closeConnection(conn); } return results; }
Example 2
Source File: UserDao.java From StudentManagement with MIT License | 6 votes |
public boolean userIsExist(String username) { Connection conn = DBUtils.getConnection(); String sql = "select * from user where username = ?"; try{ //获取PreparedStatement对象 PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ps.setString(1, username);//给用户对象属性赋值 ResultSet rs = ps.executeQuery(); if (rs.next()) { //数据库中存在此用户 return true; } //释放资源 rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); }finally { DBUtils.closeConnection(conn); } return false; }
Example 3
Source File: UserDao.java From StudentManagement with MIT License | 6 votes |
public ArrayList<User> query_all_user() { Connection conn = DBUtils.getConnection(); String sql = "select * from user order by username;"; ArrayList<User> results = new ArrayList<User>(); try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()){ User temp = new User(); temp.setUsername(rs.getString("username")); temp.setPassword(rs.getString("password")); temp.setLevel(rs.getString("level")); results.add(temp); } //关闭资源 rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); }finally { DBUtils.closeConnection(conn); } return results; }
Example 4
Source File: DepartmentDao.java From StudentManagement with MIT License | 6 votes |
public ArrayList<Department> query_all_department() { Connection conn = DBUtils.getConnection(); String sql = "select * from department order by dno;"; ArrayList<Department> results = new ArrayList<Department>(); try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()){ Department temp = new Department(); temp.setDno(rs.getString("Dno")); temp.setDname(rs.getString("Dname")); results.add(temp); } rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtils.closeConnection(conn); } return results; }
Example 5
Source File: ClassDao.java From StudentManagement with MIT License | 6 votes |
public ArrayList<Class> query_all_class() { Connection conn = DBUtils.getConnection(); String sql = "select * from class order by clno;"; ArrayList<Class> results = new ArrayList<Class>(); try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { Class temp = new Class(); temp.setClno(rs.getString("clno")); temp.setClname(rs.getString("clname")); temp.setDno(rs.getString("dno")); results.add(temp); } // 关闭资源 rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtils.closeConnection(conn); } return results; }
Example 6
Source File: StudentDao.java From StudentManagement with MIT License | 6 votes |
public ArrayList<Student> query_all_student() { Connection conn = DBUtils.getConnection(); String sql = "select * from student order by sno;"; ArrayList<Student> results = new ArrayList<Student>(); try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { Student temp = new Student(); temp.setSno(rs.getString("sno")); temp.setSname(rs.getString("sname")); temp.setSsex(rs.getString("ssex")); temp.setSage(rs.getInt("sage")); temp.setClno(rs.getString("clno")); results.add(temp); } // 关闭资源 rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtils.closeConnection(conn); } return results; }
Example 7
Source File: UserDao.java From StudentManagement with MIT License | 5 votes |
public User login(String username,String password) { Connection conn = DBUtils.getConnection(); User user = null; String sql = "select * from user where username = ? and password = ?;"; try { //获取PreparedStatement对象 PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); //对sql参数进行动态赋值 ps.setString(1, username); ps.setString(2, password); ResultSet rs = ps.executeQuery();//查询结果集 //判断数据库中是否存在该用户 if(rs.next()){ user = new User();//实例化一个user对象 //给用户对象赋值 user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setLevel(rs.getString("level")); } //释放资源 rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); }finally { DBUtils.closeConnection(conn); } return user; }
Example 8
Source File: SCDao.java From StudentManagement with MIT License | 5 votes |
public ArrayList<SC> query_all_sc() { Connection conn = DBUtils.getConnection(); String sql = "select student.sno sno,sname,ssex,sage,course.cno,cname,grade from sc,student,course where sc.sno = student.sno and course.cno = sc.cno order by sno;"; ArrayList<SC> results = new ArrayList<SC>(); try { PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { SC temp = new SC(); temp.setSno(rs.getString("sno")); temp.setSname(rs.getString("sname")); temp.setSsex(rs.getString("ssex")); temp.setSage(rs.getInt("sage")); temp.setCno(rs.getString("cno")); temp.setCname(rs.getString("cname")); temp.setGrade(rs.getDouble("grade")); results.add(temp); } // 关闭资源 rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtils.closeConnection(conn); } return results; }
Example 9
Source File: MeetManagerDaoImpl.java From scada with MIT License | 5 votes |
@Override public List<Room> getAllRoom() { Connection conn=null; try{ conn=getConnection(); String sql="SELECT * FROM `scada`.`meet_room`;"; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); List<Room> lr=new ArrayList<Room>(); while(rs.next()){ Room r=new Room(); r.setMeetroom_id(rs.getInt(1)); r.setTime(rs.getString(2)); r.setUseful(rs.getString(3)); r.setNum(rs.getInt(4)); lr.add(r); r=null; } conn.close(); conn=null; return lr; }catch(Exception e) { e.printStackTrace(); } return null; }
Example 10
Source File: MeetManagerDaoImpl.java From scada with MIT License | 5 votes |
@Override public String queryRoomUseful(String id) { String sql=null; Connection conn=null; String useful=null; try{ conn=getConnection(); sql="SELECT `useful` FROM scada.meet_room WHERE `meetroom_id`="+id; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); while(rs.next()){ useful=rs.getString("useful"); } conn.close(); conn=null; return useful; }catch(Exception e){ } return useful; }
Example 11
Source File: MeetManagerDaoImpl.java From scada with MIT License | 5 votes |
@Override public String check(String id){ Connection conn=null; String sql=null; String sql_1=null; try{ conn=getConnection(); sql="SELECT `meet_id` FROM scada.meet_manager WHERE meet_id="+id; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); while(rs.next()){ sql_1=rs.getString("meet_id"); } conn.close(); return sql_1; }catch(Exception e){ e.printStackTrace(); } return sql_1; }
Example 12
Source File: AchievementDaoImpl.java From scada with MIT License | 5 votes |
public List<MyInformation> getAllinformation(){ Connection conn=null; try{ conn=getConnection(); String sql="SELECT * FROM `scada`.`information`"; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); List<MyInformation> lr=new ArrayList<MyInformation>(); while(rs.next()){ MyInformation r=new MyInformation(); r.setId(rs.getInt("id")); r.setInformation(rs.getString("text")); lr.add(r); r=null; } conn.close(); conn=null; return lr; }catch(Exception e) { e.printStackTrace(); } return null; }
Example 13
Source File: FileManagerDaoImpl.java From scada with MIT License | 4 votes |
@Override public void save() { try { ActionContext actionContext = ActionContext.getContext(); Map<String, Object> session = actionContext.getSession(); int j = 1; String []file=new String[20]; file[0]=(String) session.get("sname"); file[1]=(String) session.get("identity_card"); file[2]=(String) session.get("sex"); file[3]=(String) session.get("both"); file[4]=(String) session.get("age"); file[5]=(String) session.get("location"); file[6]=(String) session.get("zip_code"); file[7]=(String) session.get("nation"); file[8]=(String) session.get("politics_status"); file[9]=(String) session.get("school"); file[10]=(String) session.get("education_background"); file[11]=(String) session.get("major"); file[12]=(String) session.get("job_title"); file[13]=(String) session.get("job_title_date"); file[14]=(String) session.get("qualification"); file[15]=(String) session.get("telephone"); file[16]=(String) session.get("mobilephone"); file[17]=(String) session.get("urgent_phone"); file[18]=(String) session.get("company_email"); file[19]=(String) session.get("sole_email"); int age=Integer.valueOf(file[4]); Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/scada"; String username = "root"; String password = "root"; Connection conn = (Connection) DriverManager.getConnection(url , username , password); if (conn != null) System.out.println("���ݿ����ӳɹ�!"); else System.out.println("���ݿ�����ʧ��!"); //��ɺ�ǵùر����ݿ����� String sql_1="SELECT `sid` FROM `scada`.`file_manager`;"; PreparedStatement ps_1 = (PreparedStatement) conn.prepareStatement(sql_1); ResultSet s_1=ps_1.executeQuery(); int sid; while(s_1.next()){ sid =s_1.getInt(1); System.out.println(sid); if(sid==0) { System.out.println(sid); }else j=sid+1; } /*String sql_2="SELECT `identity_card` FROM `big_project`.`file_manager` WHERE identity_card="+"'"+file[1]+"';"; PreparedStatement ps_2 = (PreparedStatement) conn.prepareStatement(sql_2); ResultSet s_2=ps_2.executeQuery(); System.out.println(6); if(s_2==null){ System.out.println(7);*/ String sql= "INSERT INTO `scada`.`file_manager` (`sid`, `sname`, `identity_card`, `sex`, `both`, `age`, `location`, `zip_code`, `nation`, `politics_status`, `school`, `education_background`, `major`, `job_title`, `job_title_date`, `qualification`, `telephone`, `mobilephone`, `urgent_phone`, `company_email`, `sole_email`) " + "VALUES ('"+j+"', "+"'"+file[0]+"', '"+file[1]+"', '"+file[2]+"', '"+file[3]+"', '"+age+"', '"+file[5]+"', '"+file[6]+"', '"+file[7]+"', '"+file[8]+"', '"+file[9]+"', '"+file[10]+"', '"+file[11]+"', '"+file[12]+"', '"+file[13]+"', '"+file[14]+"', '"+file[15]+"', '"+file[16]+"', '"+file[17]+"', '"+file[18]+"', '"+file[19]+"');"; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); int row = ps.executeUpdate(); if(row > 0) System.out.println("�ɹ������" + row + "������"); //}else{ conn.close(); conn=null; //} }catch(Exception e){ System.out.println(e); } }
Example 14
Source File: MeetManagerDaoImpl.java From scada with MIT License | 3 votes |
public String query(String id){ String sql=null; String sql_2=null; Connection conn=null; try{ conn=getConnection(); sql="SELECT `meetroom_num` FROM scada.meet_manager WHERE meet_id="+id; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); while(rs.next()){ sql_2=rs.getString("meetroom_num"); } conn.close(); conn=null; return sql_2; }catch(Exception e){ System.out.println(e); } return sql_2; }
Example 15
Source File: AchievementDaoImpl.java From scada with MIT License | 2 votes |
@Override public void save() { ActionContext actionContext = ActionContext.getContext(); Map<String, Object> session = actionContext.getSession(); String infor=(String) session.get("information"); try{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/scada"; String username = "root"; String password = "root"; Connection conn = (Connection) DriverManager.getConnection(url , username , password); if (conn != null) System.out.println("���ݿ����ӳɹ�!"); else System.out.println("���ݿ�����ʧ��!"); String sql_1="SELECT `id` FROM `scada`.`information`;"; PreparedStatement ps_1 = (PreparedStatement) conn.prepareStatement(sql_1); ResultSet s_1= ps_1.executeQuery(); int id=0; while(s_1.next()){ id =s_1.getInt("id"); } int id_1=id+1; String sql_2="INSERT INTO `scada`.`information` (`id`,`text`) VALUES ('"+id_1+"','"+infor+"')"; PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql_2); int row = ps.executeUpdate(); if(row > 0) System.out.println("�ɹ������" + row + "������"); conn.close(); conn=null; }catch(Exception e){ e.printStackTrace(); } }