jdbc链接数据库
2017-06-06 21:31
375 查看
获得数据库的连接
对数据库进行增删改查操作
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; } }
相关文章推荐
- jdbc链接各种数据库
- jdbc.properties 包含多种数据库驱动链接的版本。
- java中jdbc链接数据库大全
- J2SE基础夯实系列之JDBC,链接数据库实例代码
- J2SE基础夯实系列之JDBC,链接数据库实例代码
- 常用数据库JDBC链接写法
- JDBC链接数据库
- JDBC编程-----数据库的链接
- JDBC编程-----数据库的链接
- jdbc链接数据库(oracle10g)
- jdbc.properties 包含多种数据库驱动链接的版本
- jdbc 链接sqlserver2008的数据库设置
- jdbc 链接sqlserver2008的数据库设置
- jdbc链接数据库的三种方式
- [Java] JDBC_ODBC 链接 Excle 数据库
- jdbc链接数据库大全
- JDBC 数据库常用连接 链接字符串
- jdbc 链接数据库基础若干总结
- jdbc.properties 包含多种数据库驱动链接
- JDBC 链接Mysql 数据库详细实例