jdbc访问方法
来源:互联网 发布:上海大学乐乎社区 圈子 编辑:程序博客网 时间:2024/04/28 14:54
package com.ss;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Oper {public static String driver = "com.mysql.jdbc.Driver";public static String url = "jdbc:mysql://localhost:3306/tt";public static String username = "root";public static String password = "×××××";public static Connection getConn() {Connection conn = null;try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {conn = (Connection) DriverManager.getConnection(url, username, password);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void query() {Connection conn = getConn();String sql = "select * from student";sql = "select connection_id()";PreparedStatement pstmt = null;ResultSet rs = null;try {pstmt = conn.prepareStatement(sql);rs = pstmt.executeQuery();int col = rs.getMetaData().getColumnCount();for (int i = 0; i < col; i++) {System.out.printf("%20s\t", rs.getMetaData().getColumnName(i + 1));}System.out.println();while (rs.next()) {for (int i = 0; i < col; i++) {System.out.printf("%20s\t", rs.getObject(i + 1));}System.out.println();}rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {pstmt.close();conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void insert() {Connection conn = getConn();try {conn.setAutoCommit(false);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String sql = "insert into student values(?, ?, ?, ?)";PreparedStatement pstmt = null;ResultSet rs = null;try {pstmt = conn.prepareStatement(sql);pstmt.setInt(1, 4);pstmt.setString(2, "pp2");pstmt.setString(3, "aa2");pstmt.setString(4, "2017-01-02");int i = pstmt.executeUpdate();System.out.println("result = " + i);conn.commit();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {pstmt.close();conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void update() {Connection conn = getConn();try {conn.setAutoCommit(false);} catch (SQLException e1) {e1.printStackTrace();}String sql = "update student set name='aaa' where stud_id=1";PreparedStatement pstmt = null;try {pstmt = conn.prepareStatement(sql);int i = pstmt.executeUpdate();System.out.println("result = " + i);conn.commit();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {pstmt.close();conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void delete() {Connection conn = getConn();try {conn.setAutoCommit(false);} catch (SQLException e1) {e1.printStackTrace();}String sql = "delete from student where stud_id=1";PreparedStatement pstmt = null;try {pstmt = conn.prepareStatement(sql);int i = pstmt.executeUpdate();System.out.println("result = " + i);conn.commit();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {pstmt.close();conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();
}}}public static void main(String[] args) {//insert();//update();//delete();query();}}
阅读全文
0 0
- jdbc访问方法
- jdbc访问各数据库的方法
- JDBC新对象CachedRowSetImpl访问数据方法
- spring用jdbc访问oracle数据库方法
- myeclipse 使用JDBC方法直接访问sql2005
- jdbc访问数据库的不同方法
- 使用JDBC访问MySql的一个公共方法的解决方案
- JDBC访问数据中Statement接口的常用方法
- 使用JDBC访问MySql的一个公共方法的解决方案
- JDBC相关API以及JSP访问数据库方法
- JDBC-ODBC访问数据库,JDBC访问数据库
- 使用jdbc访问数据库
- Java JDBC访问MySql
- JDBC访问DB2
- 使用jdbc访问数据库
- JDBC访问数据库
- 使用JDBC访问数据库
- JDBC数据访问接口
- 移动测试和Web测试的区别
- yarn-client、yarn-cluster 的区别
- Android studio突然报错Error:null value in entry: incrementalFolder=null
- Spring的bean管理(注解创建对象)
- 【转】XML与HTML的区别
- jdbc访问方法
- 七牛云实时在线转码 m3u8格式
- jsp访问或者回到上级路径
- ngixn重新读入配置文件报错
- 四大组件之Service
- 开启Mysql慢查询日志
- C#----用实例来理解IComparable和IComparer
- Linux运维工程师成长路线及应实现的目标
- ubuntu中安装和卸载svn服务器