標籤:ast nal ada bool sql ret ora .exe rest
1、具體的代碼實現
import java.sql.*;public class DatabaseService {/** * Create Connection * * @param dbtype * @param username * @param password * @param url * @return * @throws Exception */public static Connection connectDBDriver(String dbtype, String username,String password, String url) throws Exception {Connection conn = null;try {if (dbtype.equals("mysql")) {Class.forName("com.mysql.jdbc.Driver").newInstance();} else if (dbtype.equals("oracle")) {Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();} else {}conn = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();}return conn;}/** * close DB * * @param conn * @throws Exception */public void closeDBDriver(Connection conn) throws Exception {try {conn.close();} catch (Exception e) { /* ignore close errors */e.printStackTrace();}}/** * get ResultSet * * @param conn * @param sql * @return * @throws Exception */private static ResultSet getResultSet(Connection conn, String sql)throws Exception {ResultSet resultSet = null;try {// PreparedStatement pstmt;// ResultSet rset;//任意的前後滾動;設定為唯讀類型的參數Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);// pstmt = conn.prepareStatement(sql);resultSet = statement.executeQuery(sql);} catch (Exception e) {e.printStackTrace();}return resultSet;}/** * get ColumnCount * * @param resultSet * @return * @throws Exception */private static int getColumnCount(ResultSet resultSet) throws Exception {int columnCount = 0;try {// ResultSet resultSet = this.getResultSet(conn, sql);//getMetaData() 擷取此 ResultSet 對象的列的編號、類型和屬性//getColumnCount() 返回此 ResultSet 對象中的列數。columnCount = resultSet.getMetaData().getColumnCount();if (columnCount == 0) {}} catch (Exception e) {e.printStackTrace();}return columnCount;}/** * get ColumnCount * * @param conn * @param sql * @return * @throws Exception */public static int getColumnCount(Connection conn, String sql) throws Exception {int columnCount = 0;try {// ResultSet resultSet = this.getResultSet(conn, sql);columnCount = getResultSet(conn, sql).getMetaData().getColumnCount();if (columnCount == 0) {}} catch (Exception e) {e.printStackTrace();}return columnCount;}/** * get RowCount * * @param conn * @param sql * @return * @throws Exception */public int getRowCount(Connection conn, String sql) throws Exception {int rowCount = 0;try {ResultSet resultSet = getResultSet(conn, sql);// booleanlast() 將游標移動到此 ResultSet 對象的最後一行。resultSet.last();// booleanlast() 將游標移動到此 ResultSet 對象的最後一行。rowCount = resultSet.getRow();if (rowCount == 0) {}} catch (Exception e) {e.printStackTrace();}return rowCount;}/** * get RowCount * * @param resultSet * @return * @throws Exception */private static int getRowCount(ResultSet resultSet) throws Exception {int rowCount = 0;try {resultSet.last();rowCount = resultSet.getRow();if (rowCount == 0) {}} catch (Exception e) {e.printStackTrace();}return rowCount;}/** * get data by row index and col index * * @param conn * @param sql * @param row * @param col * @return * @throws Exception */public static String getData(Connection conn, String sql, int row, int col)throws Exception {String data = null;int rownum = 0;int rowcount = 0;int colcount = 0;try {ResultSet resultSet = getResultSet(conn, sql);colcount = getColumnCount(resultSet);rowcount = getRowCount(resultSet);//beforeFirst() 將游標移動到此 ResultSet 對象的開頭,正好位於第一行之前。resultSet.beforeFirst();if (rowcount > 0) {if (row <= 0 || row > rowcount) {} else {if (col <= 0 || col > colcount) {} else {while (resultSet.next()) {rownum++;if (rownum == row) {data = resultSet.getString(col);break;}}}}} else {}} catch (Exception e) {e.printStackTrace();}return data;}/** * get data by row index and col index * * @param conn * @param sql * @param row * @param field * @return * @throws Exception */public static String getData(Connection conn, String sql, int row, String field)throws Exception {String data = null;int rownum = 0;int rowcount = 0;// int colcount = 0;try {ResultSet resultSet = getResultSet(conn, sql);// colcount = getColumnCount(resultSet);rowcount = getRowCount(resultSet);resultSet.beforeFirst();if (rowcount > 0) {if (row <= 0 || row > rowcount) {} else {while (resultSet.next()) {rownum++;if (rownum == row) {data = resultSet.getString(field);break;}}}} else {}} catch (Exception e) {e.printStackTrace();}return data;}// executeUpdate方法可以執行新增、更新、刪除三種sql語句public static int executeUpdate(Connection conn, String sql) {Statement stmt = null;try {stmt = conn.createStatement();stmt.executeUpdate(sql);int updateCount = stmt.getUpdateCount();return updateCount;} catch (Exception e) {e.printStackTrace();} finally {if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}}return 0;}}
2、調用函數實現增刪改查
public class JdbcData { public static void main(String[] args) { Connection connection = databaseService.connectDBDriver("mysql","username","password","URL"); String sql = "XXXX‘"; //查詢資料,擷取到資料 String data = DatabaseService.getData(connection, sql,1,1); String sqlstatus = "XXXX"; //update資料 DatabaseService.executeUpdate(connection,sqlstatus); }}
Java串連MySQL資料庫,並進行增刪改查