Java Learning---Connecting database operations

Source: Internet
Author: User

Java Connection to Oracle database

1  PackageCom.ftl.mysql;2 Importjava.sql.Connection;3 ImportJava.sql.DriverManager;4 Importjava.sql.PreparedStatement;5 ImportJava.sql.ResultSet;6 7  Public classHELLOFTL {8      Public Static FinalString dbdriver= "Oracle.jdbc.driver.OracleDriver";9      Public Static FinalString dburl= "Jdbc:oracle:thin: @localhost: 1521:impdb";Ten      Public Static FinalString dbuser= "Test"; One      Public Static FinalString dbpass= "ch_123"; A      -      Public Static voidMain (string[] args)throwsException { -System.out.println ("--------------------------------------"); theConnection conn =NULL; -PreparedStatement pstmt =NULL; -ResultSet rs =NULL; -String sql =NULL; + Class.forName (dbdriver); -conn =drivermanager.getconnection (Dburl, DBUSER, dbpass); + //sql = "SELECT * from teachers"; Asql = "Select Tname from teachers where tid =?" and score =? "; at             intTID = 1; -             intScore = 100; -String name =NULL; -PSTMT =conn.preparestatement (SQL); -Pstmt.setint (1, TID); -Pstmt.setint (2, score); inrs =pstmt.executequery (); -              while(Rs.next ()) to             { + //int tid = rs.getint (1); - //String tname = rs.getstring (2); the //String sex = rs.getstring (3); * //Float score = rs.getfloat (4); $ //System.out.println ("tid:" + tid + "\ t tname:" + tname +Panax Notoginseng //"\ t sex:" + sex + "\ T score:" + score); -Name = rs.getstring (1); the System.out.println (name); +             } A              theSystem.out.println ("--------------------------------------"); +     } -};
View Code

Java Connection MySQL Database

1  PackageCn.mldn.lxh;2 Importjava.sql.Connection;3 ImportJava.sql.DriverManager;4  Public classDatabaseConnection {5      Public Static FinalString dbdriver= "Com.mysql.jdbc.Driver";6      Public Static FinalString dburl= "Jdbc:mysql://localhost:3306/runoob";7      Public Static FinalString dbuser= "Test";8      Public Static FinalString dbpassword= "changeme_123";9     Ten     PrivateConnection Conn; One      PublicDatabaseConnection ()throwsException { A Class.forName (dbdriver); -          This. conn =drivermanager.getconnection (Dburl,dbuser,dbpassword); -     } the      PublicConnection getconnection () { -         return  This. Conn; -     } -      Public voidClose ()throwsException { +         if( This. conn! =NULL){ -             Try{ +                  This. Conn.close (); A}Catch(Exception e) { at                 Throwe; -             } -         } -     } -}
View Code

Jar Package Download

[More reference] http://www.runoob.com/java/java-mysql-connect.html

Java Learning---Connect to database operations

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.