jdbc链接数据库

xiaoxiao2021-02-28  146

获得数据库的连接

public class connectFactory { private static String name=""; private static String user=""; private static String password=""; private static String url=""; static Connection conn=null; static{ name="oracle.jdbc.driver.OracleDriver"; user="HQ133"; password="HQ133"; url="jdbc:oracle:thin:@localhost:1521:XE"; } public static Connection getConnection(){ if(conn==null){ try { Class.forName(name); conn=DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return conn; } public static void main(String []args){ Connection conn = connectFactory.getConnection(); if(conn==null){ System.out.println("--null--"); }else{ System.out.println("--not null--"); } } }

对数据库进行增删改查操作

public class userDao { public int yanZheng(String name,String password){ Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; int count=0; conn=connectFactory.getConnection(); if(conn==null){ System.out.println("ddddddd"); } String sql="SELECT count(*) FROM users where name=? and password=?"; try { pstmt=conn.prepareStatement(sql); pstmt.setString(1,name); pstmt.setString(2, password); rs=pstmt.executeQuery(); if(rs.next()){ count=rs.getInt(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(rs!=null){ try { rs.close(); } catch (SQLException e) { count=0; } } if(pstmt!=null){ try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return count; } public List select(){ Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; List<Student> list=new ArrayList<Student>(); conn=connectFactory.getConnection(); String sql="SELECT * FROM info"; try { pstmt=conn.prepareStatement(sql); rs=pstmt.executeQuery(); while(rs.next()){ Student stu=new Student(); stu.setId(rs.getInt(1)); stu.setName(rs.getString(2)); stu.setAge(rs.getInt(3)); stu.setSex(rs.getString(4)); list.add(stu); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } public int delete(int id){ Connection conn=null; PreparedStatement pstmt=null; int count=0; conn=connectFactory.getConnection(); String sql="DELETE FROM INFO WHERE id=?"; try { pstmt=conn.prepareStatement(sql); pstmt.setInt(1,id); count=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(pstmt!=null){ try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return count; } public int update(int id,String name,int age,String sex){ Connection conn=null; PreparedStatement pstmt=null; int count=0; conn=connectFactory.getConnection(); String sql="UPDATE info set name=?,age=?,sex=? WHERE id=?"; try { pstmt=conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setString(3, sex); pstmt.setInt(4, id); count=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(pstmt!=null){ try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return count; } }
转载请注明原文地址: https://www.6miu.com/read-24301.html

最新回复(0)