当前位置:网站首页>Android uses JDBC to connect to a remote database
Android uses JDBC to connect to a remote database
2022-07-24 10:12:00 【Deciduous ex】
Android Use Jdbc Connect to remote database
as everyone knows ,Android Is based on Java Compiling , And its core class library also contains most JDK Class library of . So theoretically Java The functions that can be realized are Android It's all possible . This includes calling the server-side database .
Guide pack
stay AS On gradle It can help us easily complete the work of guiding packages . The source of the package can be found in mavan Search the warehouse .
implementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.42'Connect to database
First, you need to register the driver class , Usually you only need to register once , But if you need to keep the connection in the background for a long time, it is recommended to register every time you perform the operation
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// Registration failed
e.printStackTrace();
}Next, connect to the remote database , Remember, it's best to do it in the sub thread
Connection conn = DriverManager.getConnection(url, userName, passWord);Create a declaration object , For operating the database
Statement stmt = conn.createStatement();perform SQL Modify the statement
String sql = "insert into table ...";
stmt.execute(sql);
stmt.close();
conn.close();perform SQL Query statement
String sql = "select * from " + tableName;
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Person p = new Person(re.getInt(1),re.getString("name"))
}
rs.close();
stmt.close();
conn.close();summary
The overall use is very simple , It should be noted that there is driver registration , And if that's done SQL You need to release resources after the statement , Next time you connect, you'd better re register the next driver .
边栏推荐
- Centos7 install mysql8.0
- Redis configuration serialization
- Scan line, weight segment tree
- The most complete solution for distributed transactions
- MySQL status view qps/tps/ cache hit rate view
- CRC Coding in C language
- Source insight 3.5 comment garbled
- Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)
- Rust tokio:: task:: localset running mode
- [sword finger offer II 115. reconstruction sequence]
猜你喜欢

Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)

The heads of the five major international institutions called for urgent action to deal with the global food security crisis

Interpretation of websocket protocol -rfc6455

Spark Learning: build SQL to meet the specified optimization rules

Tencent 5g innovation center was established, laying out key directions such as unmanned ports, smart mines and E-sports events

Dynamic planning: robbing families and houses
![[STM32 learning] (22) STM32 realizes 360 degree rotary encoder](/img/8e/fb036296ec3aff5e60acee5018943c.png)
[STM32 learning] (22) STM32 realizes 360 degree rotary encoder

Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K
![CAS principle [concurrent programming]](/img/f0/77e7e1079f70198c601b0f1e25106e.png)
CAS principle [concurrent programming]

Home raiding III (leetcode-337)
随机推荐
The best time to buy and sell stocks Ⅲ (leetcode-123)
2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
Add a love power logo to your website
What did zoneawareloadbalancer of ribbon and its parent class do?
Trie tree template 2
757. Set the intersection size to at least 2: greedy application question
Curse of knowledge
Application of for loop
[STM32 learning] (17) STM32 realizes LCD12864 display serial implementation
多表查询之子查询_单行单列情况
The best time to buy and sell stocks includes handling charges (leetcode-714)
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
What happens from the input URL to the page load
Spark Learning: build SQL to meet the specified optimization rules
Interpretation of websocket protocol -rfc6455
Arduino drive lcd1602a
【LeeCode】获取2个字符串的最长公共子串
Homologous policy solutions
[C language] implementation of three versions of address book small project (including source code)
Sub query of multi table query_ Single row and single column