Java Connection Oracle/mysql Database tutorial

Source: Internet
Author: User

To import a package:
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Declaring variables:
private static String Usernamr = "LSDB"; Database user name, in MySQL all database user name password is the same, Oracle database user name password is set separately
private static String PASSWORD = "lsdb123#"; Database Password
private static String Oracle_drvier = "Oracle.jdbc.OracleDriver"; Oracle Database driven notation
private static String Mysql_drvier = "Com.mysql.jdbc.Driver"//mysql database-driven notation
private static String Oracle_url = "Jdbc:oracle:thin:@192.168.220.128:1521:lsdb"; Oracle database URL notation, LSDB is the database to connect to
private static String Mysql_url = "Jdbc:mysql://127.0.0.1:3306/banksystem"; MySQL database URL notation, Banksystem is the database to connect to

Connection:
Class.forName (Drvier);
Connection conn = drivermanager.getconnection (URL, USERNAMR, PASSWORD);

Operation:
String sql = "INSERT into Userx values (?,?)"; The SQL statement to execute
PreparedStatement pstm = conn.preparestatement (sql); Precompiled objects
Pstm.setstring (1, username); 1 indicates the contents of the first question mark
Pstm.setstring (2, password); 2 indicates the contents of the first question mark
Pstm.executeupdate (); Change and use
ResultSet rs = Pstm.executequery (); Check Use
while (Rs.next ()) {
int userid = rs.getint ("userid"); Take the "userid" column in the query result collection as an integral type
String Password = rs.getstring ("password"); Fetch the "password" column in the query result collection as a string type
}

Description

Thus, it can be seen that the writing format of the database is determined by the programming language and not by the database; The language style of the same language operation is basically consistent with the different database.

Java Connection Oracle/mysql Database tutorial

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.