zoukankan      html  css  js  c++  java
  • application下的JDBC操作

    package com.zss.www;

    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;
    import com.mysql.jdbc.Statement;

    public class tableCRUD {

    public static void main(String[] args) throws Exception {

    /*String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/student";
    String username = "root";
    String password = "711109";
    Connection conn = null;
    Class.forName(driver);
    conn = (Connection) DriverManager.getConnection(url, username, password);
    Statement stmt=null;
    ResultSet rs=null;
    stmt=(Statement) conn.createStatement();
    rs=stmt.executeQuery("select * from information");
    while (rs.next()){
    System.out.println("name="+rs.getString(1));
    System.out.println("name="+rs.getString(2));
    }

    rs.close();
    stmt.close();
    conn.close();*/


    /*Student one=new Student("mary",10);
    selectAll();
    update(one);
    selectAll();
    delete(one);
    selectAll();
    insert(one);
    selectAll();*/
    String username01 = "chenjirong";
    int age01 = 20;
    ResultSet rs = myquery(
    "select * from information where name = ? and age = ?",
    username01, age01);
    while (rs.next()) {
    System.out.println(rs.getString(1));
    System.out.println(rs.getString(2));
    }



    username01 = "myfatpig";
    age01 = 45;
    myinsert(
    "insert into information (name,age) values(?,?)",
    username01, age01);


    }






    private static Connection getConn() {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/student";
    String username = "root";
    String password = "711109";
    Connection conn = null;
    try {
    Class.forName(driver);
    conn = (Connection) DriverManager.getConnection(url, username, password);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return conn;
    }


    private static int insert(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "insert into information (name,age) values(?,?)";
    PreparedStatement pstmt;
    try {
    pstmt = (PreparedStatement) conn.prepareStatement(sql);
    pstmt.setString(1, student.getName());
    pstmt.setInt(2, student.getAge());
    i = pstmt.executeUpdate();
    pstmt.close();
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return i;
    }


    private static int update(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "update information set age="+String.valueOf(student.getAge()+20)+" 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;
    }

    private static Integer selectAll() {
    Connection conn = getConn();
    String sql = "select * from information";
    System.out.println("************");
    PreparedStatement pstmt;
    try {
    pstmt = (PreparedStatement)conn.prepareStatement(sql);
    ResultSet rs = pstmt.executeQuery();
    int col = rs.getMetaData().getColumnCount();//number of fields
    System.out.println("************");
    while (rs.next()) {
    for (int i = 1; i <= col; i++) {
    System.out.print(rs.getString(i) + " ");
    if ((i == 2) && (rs.getString(i).length() < 8)) {
    System.out.print(" ");
    }
    }
    System.out.println("");
    }
    System.out.println("============================");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return null;
    }

    private static int delete(Student student) {
    Connection conn = getConn();
    int i = 0;
    String sql = "delete from information 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;
    }

    private static ResultSet myquery(String sql, Object... args) throws Exception {

    Connection conn = getConn();
    PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
    for (int i = 0; i < args.length; i++) {
    pstmt.setObject(i+1, args[i]);
    }
    return pstmt.executeQuery();
    }


    private static void myinsert(String sql, Object... args) throws Exception {

    Connection conn = getConn();
    PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
    for (int i = 0; i < args.length; i++) {
    pstmt.setObject(i+1, args[i]);
    }
    pstmt.executeUpdate();
    pstmt.close();
    }

    }

  • 相关阅读:
    Unity 摄像机旋转跟随缩放控制
    Unity 协程深入解析与原理
    好看的滚动条
    ES6编译问题SyntaxError: Unexpected token import
    Axure rp8 注册码,亲测可以用! 可用给个赞呗!!
    angular 项目中遇到rxjs error TS1005:';'
    window 查看端口 杀端口
    angular 中嵌套 iframe 报错
    js 快速生成数组的方法
    ng-packagr 不能全部打包文件
  • 原文地址:https://www.cnblogs.com/bgd150809222/p/6642321.html
Copyright © 2011-2022 走看看