很开心,今天终于连接到了。界面显示的是返回的Connection的值和我查询的一个数据库内容。
主要的原因是MySQL8.0版本不兼容,反正我的是更换5.0版本就好了。(提醒大家不要不相信,我也在这里卡了好久,明明代码没有问题,但就是连接不上)
还有连接数据库是一个耗时的过程,要放在次线程进行,这个看网上好像说是新更新的Android 是这样。网上有好多代码可以看,主要是配置环境不对,真的不是不努力,被卡在第一步也是打击很大的。
简单的看一下我的代码,只是简单的实现一个查询。
MainActivity
package com.example.myapplicatio1; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import java.nio.file.FileSystemLoopException; import java.sql.Connection; public class MainActivity extends AppCompatActivity { public Connection coon = null; TextView address = null; TextView result = null; String re = null; User user =null; utilDao dao = new utilDao(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = findViewById(R.id.result); user = new User("1","1"); Thread thread =new Thread(new Runnable() { @Override public void run() { address = findViewById(R.id.add); coon = getConnection.getcoon(); address.setText(String.valueOf(coon)); re = dao.select(user); // re = "sdf"+ result.setText(re); } });thread.start(); } }
连接数据库的封装类
package com.example.myapplicatio1; import java.sql.*; public class getConnection { private static Connection connection = null; private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; private static final String DATABASE_URL = "jdbc:mysql://10.99.113.81/user"; private static final String DATABASE_USER = "Android"; private static final String DATABASE_PASSWORD = "246437"; public static Connection getcoon() { try { // Load the JDBC driver Class.forName(JDBC_DRIVER); // Establish a connection connection = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD); // You now have a connection to the database } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } }
User类,就是数据库主要的内容,我的是有三个。
package com.example.myapplicatio1; public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(String name, String password) { super(); this.name = name; this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; } }
查询数据库内容
package com.example.myapplicatio1; import java.sql.SQLException; import java.sql.*; public class utilDao { String identity; //查询 public String select(User user) { Connection coon = getConnection.getcoon(); String sql = "select identity from information where name=? and password = ? "; try { PreparedStatement ps = coon.prepareStatement(sql); ps.setString(1, user.getName()); ps.setString(2, user.getPassword()); ResultSet re = ps.executeQuery(); while(re.next()) { identity = re.getString("identity"); } } catch (SQLException e) { e.printStackTrace(); return null; } return identity; } }
这个是我的navicat一个数据库图形化工具。
也有好久了,明天终于可以开始后面的进度了。
标签:String,re,二十一日,三月,import,password,public,name From: https://www.cnblogs.com/2022-yang/p/18088490