我们的应用应该连接到SQL数据库。它在我们的网络中。应用程序应编辑数据库中的数据。我们已经建立了连接,并希望将onclicklistener设置为Button,导致连接代码连接。
这是我们得到的代码:
public class Werte_aendern extends AppCompatActivity {
TextView tvIP;
String Textauslesen = tvIP.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tvIP = (TextView) findViewById(R.id.tvIP);
setContentView(R.layout.activity_werte_aendern);
}
Connection con = null;
//private static String dbHost = "192.168.40.148"; // Hostname
String dbPort = "3306"; // Port -- Standard: 3306
String dbName = "wasserwerte"; // Datenbankname
String dbUser = "App"; // Datenbankuser
String dbPass = "fruitcake"; // Datenbankpasswort
private Werte_aendern(){
try {
Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden.
// Verbindung zur JDBC-Datenbank herstellen.
con = DriverManager.getConnection("jdbc:mysql://"+Textauslesen+":"+ dbPort+"/"+dbName+"?"+"user="+dbUser+"&"+"password="+dbPass);
// Statement createStatement();
// SQLiteDatabase wasserwerte =
} catch (ClassNotFoundException e) {
Toast.makeText(getApplicationContext(), "Treiber nicht gefunden", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(getApplicationContext(), "Verbindung nicht möglich", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "SQLException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "SQLState: " + e.getSQLState(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "VendorError: " + e.getErrorCode(), Toast.LENGTH_SHORT).show();
}
}
}
我们是新手,但我们必须为学校项目做这件事 你能帮帮我们吗?
答案 0 :(得分:1)
我已更新您的代码。应始终将活动命名为[Whatever]活动。如果" WerteAendern"是一个正确的名字,因为我不会说德语(我认为它是德语)。
public class WerteAendernActivity extends AppCompatActivity {
TextView tvIP;
// You should get the text from the View AFTER inflating the layout and find it with
// findViewById. Otherwise it's gonna crash.
String textauslesen;
private Button connectBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// First you inflate the layout
setContentView(R.layout.activity_werte_aendern);
// Then you get the views
tvIP = (TextView) findViewById(R.id.tvIP);
// Probably should be somewhere else as there is no interesting text to retrieve from the
// view at the moment
textauslesen = tvIP.getText().toString();
connectBtn = (Button) findViewById(R.id.connection_button); // ! You need to add a Button in your layout
connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Add some test, ... if you're already connected
connectToDataBase();
}
});
}
Connection con = null;
//private static String dbHost = "192.168.40.148"; // Hostname
String dbPort = "3306"; // Port -- Standard: 3306
String dbName = "wasserwerte"; // Datenbankname
String dbUser = "App"; // Datenbankuser
String dbPass = "fruitcake"; // Datenbankpasswort
// Method to connect to the database.
// !!! You're not supposed to override the constructor of an Activity!
private void connectToDataBase() {
try {
Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden.
// Verbindung zur JDBC-Datenbank herstellen.
con = DriverManager.getConnection("jdbc:mysql://" + textauslesen + ":" + dbPort + "/" + dbName + "?" + "user=" + dbUser + "&" + "password=" + dbPass);
// Statement createStatement();
// SQLiteDatabase wasserwerte =
} catch (ClassNotFoundException e) {
Toast.makeText(getApplicationContext(), "Treiber nicht gefunden", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (SQLException e) {
Toast.makeText(getApplicationContext(), "Error! See Exception logs", Toast.LENGTH_SHORT).show();
// The logs will be displayed in the Logcat window in Android Studio
e.printStackTrace();
}
}
}