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();}}


原创粉丝点击