我在教程中看到了“Class.forName”的使用。请让我知道它是做什么的,以及这段代码是否可以正常工作?
public class Conexao {
String ip = "ip";
String class = "net.sourceforge.jtds.jdbc.Driver";
String bd = "nomeBD";
String username = "UserName";
String password = "password";
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL;
try {
Class.forName(class);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";" + "databaseName=" + bd + ";user=" + username + ";password=" + password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERROR", se.getMessage());
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
return conn;
}
}
最佳答案
没有看到错误消息,并且格式可能不正确。通常我会写:
String url = "jdbc:mysql://localhost:3306/samp_db";
...
DriverManager.getConnection(url, username, password);
....
关于java - 在数据库连接中,Class.forName 的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56055409/