JDBC学习之一

xiaoxiao2021-02-28  21

package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class tsetJDBC {      public static void main(String[] args) {       ResultSet rst = null;       Statement stmt = null;       Connection conn = null;          try {            Class.forName("com.mysql.jdbc.Driver");            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_book", "root", "root");            System.out.println("connect successful");            stmt = conn.createStatement();            rst = stmt.executeQuery("select * from t_user");            while(rst.next()) {                 System.out.println(rst.getString("id"));                 System.out.println(rst.getString("userName"));                 System.out.println(rst.getString("password"));                    }       }catch (ClassNotFoundException e) {            e.printStackTrace();           }catch (SQLException e) {            e.printStackTrace();       }finally {            try {                 if(rst != null) {                     rst.close();                     rst = null;                 }                if(stmt != null) {                     stmt.close();                     stmt = null;                }                if(conn != null) {                  conn.close();                  conn = null;                 }                 }catch (SQLException e) {                 e.printStackTrace();            }       }    } }
转载请注明原文地址: https://www.6miu.com/read-2800271.html

最新回复(0)