JDBC Java database connection with simple SQL statement execution

Source: Internet
Author: User

Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement; public class Test {/** * @param args */public static void main (string[] args) {/* This part is to connect the Oracle database with Java driver, drive Jar can be downloaded on the Internet itself, you can also give me a message, send you * */try {//Operation Database First step: Find the driver Class.forName ("Oracle.jdbc.driver.OracleDr             Iver "); Step two: Establish a connection Connection con = drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:oracle", "David", "110"                     );             127.0.0.1 is the database host that needs to connect the ip,oracle is the database name, David is the login name, 110 is the password, the other parts are fixed unchanged!             Step three: Establish a Statement object Statement st = Con.createstatement ();            Fourth step: Write out the SQL statement and execute the Statement object method//String sql = "Update person set pname = ' ling ' where pname = ' ling '";//           St.executeupdate (SQL);            Additions and deletions are used this execution of the statement because of the update database String sql = "SELECT * from the person";                   ResultSet rs=st.executequery (SQL); Query with this executive languageThe result of the sentence query is saved in the result set ResultSet class object Rs in while (Rs.next ()) {                Rs.next () returns true if the result set RS has the next record, otherwise returns false int id = rs.getint ("PersonID");                String name = rs.getstring (2);                String sex = rs.getstring (3);            System.out.println (id+ "--->" +name+ "--------" +sex);                 } con.close ();        Remember to close the connection after the database operation} catch (Exception e) {e.printstacktrace (); }

  

JDBC Java database connection with simple SQL statement execution

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.