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