JDBC Link mysql database + Execute SQL

Source: Internet
Author: User
Tags stmt

Package com.wangbingan.utils;import java.sql.connection;import java.sql.drivermanager;import  JAVA.SQL.RESULTSET;IMPORT JAVA.SQL.STATEMENT;/** * JDBC Linked Database  *  * @ author administrator *  */public class jdbcutil {public static  Void main (string args[])  throws Exception {//  database connection connection conn =  null;//  Record database Data Int count = 0;// resultset object Resultset re = null ;//  driver string drive =  "com.mysql.jdbc.Driver";//  address string url =  "JDBC: Mysql://localhost:3306/shiro "+ "?username=root&password=root&useunicode=true& Characterencoding=utf8 ";//  Load Database driver Class.forName (drive);//  get database link conn =  Drivermanager.getconnection (URL);// statement has three ways to execute SQL statement stmt =  Conn.createstatement ();//  query string sql =  "SELECT&NBSp;* from vip ";//  New string sql1 = " Insert into vip (Id, name,  phone)  values (' 3 ', ' 3 ', ' 3 ') ";//  returns a Boolean value. Falseboolean result1 = stmt.execute ( SQL1);//  Returns a Boolean value. Trueboolean result = stmt.execute (SQL);//  updated number of bars INT RESULT2  = stmt.executeupdate (SQL1);//  returns resultsetre = stmt.executequery (SQL);while  ( Re.next ())  {system.out.println ("Query data ..."  +  "Name:"  + re.getstring ("name") +  "-"  +  "Tel:"  + re.getstring ("Phone")  +  "\ n"); count++;} SYSTEM.OUT.PRINTLN ("Execute added successful return value:"  + result1 +  "\ n"); System.out.println ("Executeupdate added successful return value:"  + result2 +  "\ n");if  (Result)  { SYSTEM.OUT.PRINTLN ("Database total:"  + count +  "bar data"  +  "\ n");}}

Reference website:

The difference between JDBC execution SQL three methods: http://www.blogjava.net/yxhxj2006/archive/2012/08/01/384544.html

JDBC Link mysql:http://www.cnblogs.com/cy163/archive/2008/08/22/1274413.html

Summarize: JDBC performs a query operation, uses ExecuteQuery (), performs an update operation, uses execute (), or executeupdate ().

Supplement: JDBC Query bar number, not supported, so the execution uses the counter to calculate.

JDBC Link mysql database + Execute SQL

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.