<br>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private TextView textView,textTwo;
private final static String URL = "jdbc:mysql://localhost:3306/librarydb";
static String rootName = "root";
static String PWD = "1997831";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
textTwo= (TextView) findViewById(R.id.textTwo);
try {
Class.forName("com.mysql.jdbc.Driver");
textView.setText("驱动加载成功");
} catch (ClassNotFoundException e) {
textView.setText("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Connection conn= conn = (Connection) DriverManager.getConnection(URL,rootName,PWD);
textTwo.setText("数据库连接成功!");
} catch (SQLException e) {
textTwo.setText("数据库连接失败!");
e.printStackTrace();
}
}
}).start();
}
}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4809/viewspace-3122701/ ,如需转载,请注明出处,否则将追究法律责任。