JDBC, a way to link to a database--example

Source: Internet
Author: User
Tags driver manager

Package Com.jdbcke;import java.sql.Connection; Import Java.sql.databasemetadata;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.resultsetmetadata;import Java.sql.sqlexception;import java.sql.Statement; Import Javax.swing.plaf.synth.synthstyle;public class Ke1 {public static void main (string[] args) {try {//Access database//1. Load Driver Loading the database provides the driver class Class.forName ("Oracle.jdbc.driver.OracleDriver");//2. Get the database connection,//url--the database address through the Java Driver Manager; user--User name ; password-password//Different database address is not the same connection conn= drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:o Rcltao "," test0816 "," 123456 "); SYSTEM.OUT.PRINTLN ("Connection succeeded");//3. Obtain the declaration of the Operational Database Statementstatement st=conn.createstatement ();//4. Operation data, add/subtract change//perform operation, increment int i =st.executeupdate ("INSERT into student (sno,sname,ssex)" + "values (' 1517 ', ' small army ', ' Male ')"); SYSTEM.OUT.PRINTLN ("Add data success, return value =" +i);//return value means: The number of data record bars that the operation affects. Change//int i=st.executeupdate ("Update student set ssex= ' man ' where ssex= ' 1 '");//system.out.println ("Add data success, return value =" +i);// Check resultsetThe data result set resultset rs= st.executequery ("SELECT * from student");//traverse the result set while (Rs.next ())//next () to determine if there is a next record, if any, Move the pointer to the record data {//Read data string sno=rs.getstring ("Sno"); String sname=rs.getstring ("sname"); String ssex=rs.getstring ("Ssex"); System.out.println ("sno=" +sno+ "sname=" +sname+ "ssex=" +ssex);} 3.4 Create declaration and execute preparedstatement//string sql= "select * from Kaihubiao where yhcard=? and cardmm=? "; /preparedstatement ps= conn.preparestatement (SQL)//ps.setstring (1, Cardid);//ps.setstring (2, mm);////Execution/ ResultSet rs= ps.executequery ();////traversal//rtn=rs.next ()//If there is data to verify through//supplemental knowledge//Call stored procedure//callablestatement cs= Conn.preparecall ("{Call stored procedure name (?,?)}"); /Get information related to the database DatabaseMetaData dm= conn.getmetadata (); System.out.println ("Url=" +dm.geturl ()); System.out.println ("name =" +dm.getusername ()); SYSTEM.OUT.PRINTLN ("Product name =" +dm.getdatabaseproductname ());//metadata for the result set--you can get the columns in the database table ResultSetMetaData RSMD = Rs.getmetadata (); System.out.println ("Number of columns" +rsmd.getcolumncount ()); System.out.println (column name of column +rsmd.getcolumnname (1)); System.out.println ("Number of columns" +rsmd.getcolumncount ()); Rs.close ();//close declaration, Release resource St.close ();//Close database connection Conn.close ();} catch (ClassNotFoundException | SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

JDBC, a way to link to a database--example

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.