Java connects to MySQL database and makes additional deletions and checks

Source: Internet
Author: User
Tags rowcount stmt

1, the specific code implementation

Import java.sql.*;p Ublic class Databaseservice {/** * Create Connection * * @param dbtype * @param username * @param pas Sword * @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 ("Co M.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 {Co Nn.close ();} catch (Exception e) {/* Ignore close errors */e.printstacktrace ();}} /** * Get ResultSet * * @param conn * @param SQL * @return * @throws Exception */private static ResultSet Getresultset (Co Nnection conn, String sql) throws Exception {ReSultset ResultSet = null;try {//PreparedStatement pstmt;//ResultSet rset;//arbitrary front and back scrolling; parameter set to read-only type 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 re  Sultset) throws Exception {int columnCount = 0;try {//ResultSet ResultSet = THIS.GETRESULTSET (conn, SQL);//getmetadata () Gets the number, type, and property of the column for this ResultSet object//getcolumncount () returns the number of columns in this ResultSet object. 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 (Conne Ction 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, S  Tring sql) throws Exception {int rowCount = 0;try {ResultSet ResultSet = getresultset (conn, SQL);//Booleanlast () move cursor to this The last line of the ResultSet object. Resultset.last ();//Booleanlast () moves the cursor to the last line of this ResultSet object. 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 Resultse T) throws Exception {int rowCount = 0;try {resultset.last (); rowCount = Resultset.getrow (); if (RowCount = = 0) {}} catch (Ex Ception 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 () Move the cursor to the beginning of this ResultSet object, just before the first line. Resultset.beforefirst (), if (RowCount > 0) {if (row <= 0 | | row > ROWCOUNT) {} else {if (col <= 0 | | col > C Olcount) {} else {while (Resultset.next ()) {rownum++;if (rownum = = row) {data = resultset.getstring (col); 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 Ex Ception */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 ()) {row Num++;if (rownum = = row) {data = resultset.getstring (field); break;}}} else {}} catch (Exception e) {e.printstacktrace ();} return data;} The Executeupdate method can perform new, update, and delete three SQL statements 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.print StackTrace ();}}} return 0;}}

2, call the function to implement the deletion and modification

public class Jdbcdata {public     static void Main (string[] args) {        Connection Connection = databaseservice.connectd Bdriver ("MySQL", "username", "password", "URL");        String sql = "XXXX";        Query data, get to Data        String: Databaseservice.getdata (Connection, sql,1,1);        String sqlstatus = "XXXX";        Update Data        databaseservice.executeupdate (connection,sqlstatus);}     }

  

Java connects to MySQL database and makes additional deletions and checks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.