JDBC增删改查 代码演示
来源:互联网 发布:好帮手软件简介 编辑:程序博客网 时间:2024/05/29 18:22
(2)连接的获取
在操作前必须先获取与数据库的连接。
driver、url的格式同样可以参考各种数据库对应的jar包、驱动类名和URL格式。
private static Connection getConn() { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/samp_db"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn;}
(3)insert
private static int insert(Student student) { Connection conn = getConn(); int i = 0; String sql = "insert into students (Name,Sex,Age) values(?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, student.getName()); pstmt.setString(2, student.getSex()); pstmt.setString(3, student.getAge()); i = pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i;}
(4)update
private static int update(Student student) { Connection conn = getConn(); int i = 0; String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i;}
(5)select
以select * from XXX为例。
private static Integer getAll() { Connection conn = getConn(); String sql = "select * from students"; PreparedStatement pstmt; try { pstmt = (PreparedStatement)conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); System.out.println("============================"); while (rs.next()) { for (int i = 1; i <= col; i++) { System.out.print(rs.getString(i) + "\t"); if ((i == 2) && (rs.getString(i).length() < 8)) { System.out.print("\t"); } } System.out.println(""); } System.out.println("============================"); } catch (SQLException e) { e.printStackTrace(); } return null;}
(6)delete
private static int delete(String name) { Connection conn = getConn(); int i = 0; String sql = "delete from students where Name='" + name + "'"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i;}
阅读全文
0 0
- JDBC增删改查 代码演示
- JDBC基本源码演示增删改查
- Jdbc 增删改查
- jdbc增删改查
- JDBC 增删改查
- jdbc增删改查
- jdbc增删查改
- JDBC 增删改查
- jdbc:增删改查
- jdbc---增删改查
- jdbc的增删改查
- JDBC 增删查改(1)
- JDBC 增删查改(2)
- JDBC增删改查实例
- JDBC---数据库连接增删改查
- JDBC 实现增删改查
- Struts2 jdbc 增删改查
- JDBC增删改查MVC
- Tomcat系列—JDK安装(tar.gz)
- JS 中关于document和window对象的一些值: scrollHeight,scrollLeft,scrollTop,scrollWidth等。。
- dubbox编译
- 汇编1
- HihoCoder第十周——已知前序中序求后序
- JDBC增删改查 代码演示
- css一些常用属性
- Node.js第三方模块之htmlparser2
- 汇编2
- c#算法
- HDU 5816 Hearthstone(状态压缩DP+概率)
- 单调栈的尝试
- 【for循环】打印菱形
- 算法分析—— Binary Tree Postorder Traversal