今天学习了安卓开发连接数据库的内容,
学习时间 | 2小时 |
代码量 | 150 |
package com.example.myapplication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class mysqlhelp {
public static int getUserSize(){
final String CLs="com.mysql.jdbc.Driver";
final String URL="jdbc:mysql://192.168.129.97:3306//bookdb";
final String USER="root";
final String PWD="822611";
int count=0; //用户数量
try{
Class.forName(CLs);
Connection conn= DriverManager.getConnection(URL,USER,PWD);
String sql="select count(1) as s1 from userinfo";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
count=rs.getInt("s1");
}
}catch(Exception ex){
ex.printStackTrace();
}
return count;
}
} package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
} <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
标签:11,count,2024.3,String,软工,sql,import,android,final From: https://www.cnblogs.com/luoqingci/p/18067374