In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
JDBC provides a standard for accessing a database is a series of interfaces that define a common way to access a database.
The implementation of JDBC is provided by various database vendors
Definition of JDBC interface
Database manufacturer's implementation of jdbc jar
Establish connection-send sql- to execute sql- return result-close connection
JDBC API (interface)
Connection between java.sql.Connection// encapsulation and database
Java.sql.Statement// encapsulates the execution of SQL statements
Jave.sql.ResultSet// encapsulates the result of DQL execution
Package jdbc;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import oracle.jdbc.OracleDriver;public class OracleDemo01 {public static void main (String [] args) throws Exception {/ / implementation of loading JDBC / / Driver driver = new OracleDriver () Write / / DriverManager.registerDriver (driver) by yourself; / / load the class into the static fast memory and execute the above two items without having to write / / the code in the static block of the class will register Class.forName for Driver ("oracle.jdbc.OracleDriver") / / load the JDBC implementation with this / / create a connection / / call the getConnection method of DriverManager / / this method returns the object of the database vendor's implementation class of the Connection interface (because DriverManager has registered the database vendor's Driver information) String url = "jdbc:oracle : thin:@x1.zongxuan.online:1521:xx " String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection (url,user,pass) / / url is used to represent the connection information of the database (ip port database name) different database vendors have specific url formats and identities / / DriverManager will select different driver information according to this logo / / if you register at the same time Implementation of multiple database vendors / / user / / password / / System.out.println (con) / / execute the createStatement () method of SQL / / Connection to create the object Statement stmt = con.createStatement () of the Statement implementation class / / only DQL statements can be executed / / the return value is ResultSet / / transfer the SQl statement to the database for execution / / get the result data of the database transmission / / and encapsulate this data into a ResultSet object ResultSet rs = stmt.executeQuery ("select empno Ename name,sal from emp ") / / get the result while (rs.next ()) {/ / .next () cursor down System.out.println (rs.getString ("empno") + "," + rs.getString ("name") + "," + rs.getString ("sal"));} rs.close () / / close connection}} package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class OracleDemo02 {public static void main (String [] args) throws Exception {Class.forName ("oracle.jdbc.OracleDriver") String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user= "scott"; String pass= "xxxxx"; Connection con = DriverManager.getConnection (url,user,pass); Statement stmt = con.createStatement () / / execute the DML statement insert delete update String sql = "insert into emp2 (empno,ename,sal,deptno) values"; int n = stmt.executeUpdate (sql); / / return an integer representation of the number of rows affected by the statement just now System.out.println (n); con.close ();}} package jdbc Import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class OracleDemo03 {public static void main (String [] args) throws Exception {Class.forName ("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx" Connection con = DriverManager.getConnection (url,user,pass); Statement stmt = con.createStatement (); String sql = "delete from emp2 where ename='lmdtx'"; int n = stmt.executeUpdate (sql); System.out.println (n); con.close ();}}
There is a risk of being injected.
Name:a' or 'baked goods ba' or' b'='bselect ename,empno,sal from emp2 where ename ='a'or 'b'='b'package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Scanner;public class OracleDemo04 {public static void main (String [] args) throws Exception {Scanner in = new Scanner (System.in) System.out.print ("name:"); String name = in.nextLine (); System.out.println (name); Class.forName ("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott" String pass = "xxxxx"; Connection con = DriverManager.getConnection (url,user,pass); Statement stat = con.createStatement (); String sql = "select ename,empno,sal from emp2 where ename ='" + name+ "'"; System.out.println (sql); ResultSet rs = stat.executeQuery (sql) While (rs.next ()) {System.out.println (rs.getString (1) + "," + rs.getInt (2) + "," + rs.getInt (3));} con.close ();}}
PreparedStatement interface (use this is good)
Improve efficiency
Prevent SQL Injection
1 create a connection
2PreparedStatement
PreparedStatement stmt = con.prepareStatement (sql)
3 stmt.setString (1, "xxx")
Stmt.setInt (1123)
Package jdbc;import java.io.BufferedReader;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class OracleDemo05 {public static void main (String [] args) throws Exception {BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); System.out.println ("name:"); String name = br.readLine () Class.forName ("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection (url,user,pass); String sql = "select empno,ename,sal from emp2 where ename=?" PreparedStatement stmt = con.prepareStatement (sql); / / set the value of the first question mark in sql to the string stmt.setString (1jue name); ResultSet rs = stmt.executeQuery () While (rs.next ()) {System.out.println (rs.getInt (1) + "," + rs.getString (2) + "," + rs.getInt (3));} con.close ();}} package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement Public class OracleDemo06 {public static void main (String [] args) throws Exception {Class.forName ("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection (url,user,pass) String sql = "insert into emp2 (empno,ename) values (?,?)"; PreparedStatement stmt = con.prepareStatement (sql); int n = 0; for (int I = 1000
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.