I'am trying to create an app in which a user can click the two buttons start and end to pick the current time. On the third button the user can save the data to the database. This programm is a sort of testing my code. I created the database test and the table testung with the collums id and name. In the table are two records. I want to retrieve the data from the table and display it on my textview.
If I write the Class.forName("com.mysql.jdbc.Driver"); it autocompletes the String between the (), so I dont know if there is also something wrong.
The error shows up at this line of code Connection con = DriverManager.getConnection(databaseURL, user, pw); and I dont have any clue what is wrong there.
Here is the code of the MainActivity
package at.fabian.zeiterfassungtest;
import android.content.DialogInterface;
import android.os.StrictMode;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.Calendar;
import java.sql.*;
import at.fabian.zeiterfassungtest.database.TimeDB;
public class MainActivity extends AppCompatActivity {
private static final String databaseURL = "jdbc:mysql://localhost:3306/test";
private static final String user = "fabi";
private static final String pw = "1234";
public EditText _editTextStart;
public EditText _editTextEnd;
private Button _buttonStart;
private Button _buttonEnde;
private Button _buttonSave;
private TextView _textViewOutput;
private DateFormat _dateTimeFormatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT, //Datum
DateFormat.SHORT //Uhrzeit
);
//public static TimeDB DB=new TimeDB();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_editTextStart = (EditText) findViewById(R.id.editTextTimeStart);
_editTextEnd = (EditText) findViewById(R.id.editTextTimeEnd);
_buttonStart = (Button) findViewById(R.id.buttonStart);
_buttonEnde = (Button) findViewById(R.id.buttonEnd);
_buttonSave = (Button) findViewById(R.id.buttonSave);
_textViewOutput = (TextView) findViewById(R.id.textViewOutput);
//_editTextStart.setText(Calendar.getInstance().getTime().toString());
}
@Override
protected void onResume() {
super.onResume();
_buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(
"MainActivity",
"Onclick für den Start Button"
);
Toast.makeText(
MainActivity.this,
"onClick für den Start Button",
Toast.LENGTH_SHORT
).show();
Calendar currentTime = Calendar.getInstance();
_editTextStart.setText(currentTime.getTime().toString());
_editTextStart.setText(
_dateTimeFormatter.format(currentTime.getTime())
);
}
});
_buttonEnde.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(
"MainActivity",
"Onclick für den Endzeit Button"
);
Toast.makeText(MainActivity.this,
"onClick für den Endzeit BUtton",
Toast.LENGTH_SHORT
).show();
Calendar currentTime = Calendar.getInstance();
Log.d(
"MainActivity",
currentTime.toString()
);
_editTextEnd.setText(currentTime.getTime().toString());
_editTextEnd.setText(
_dateTimeFormatter.format(currentTime.getTime())
);
}
});
_buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(
"MainActivity",
"Onclick für den Save Button"
);
Toast.makeText(MainActivity.this,
"onClick für den Save Button",
Toast.LENGTH_SHORT
).show();
testDB();
}
});
}
public void testDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(databaseURL, user, pw);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from testung");
String result =rs.toString();
_textViewOutput.setText(result);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
super.onPause();
_buttonStart.setOnClickListener(null);
_buttonEnde.setOnClickListener(null);
}
}
This is the error message
08/25 15:06:17: Launching 'app' on OnePlus ONEPLUS A6013.
$ adb shell am start -n "at.fabian.zeiterfassungtest/at.fabian.zeiterfassungtest.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 19942 on device 'oneplus-oneplus_a6013-b41d8bd2'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
I/iterfassungtes: [GL_OOM] ClampGrowthLimit 268435456
V/Font: Change font:1
V/Font: Default family:android.graphics.Typeface@391c1c1d
E/Perf: Fail to get file list at.fabian.zeiterfassungtest
getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
W/iterfassungtes: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/iterfassungtes: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
V/ViewRootImpl: The specified message queue synchronization barrier token has not been posted or has already been removed
I/AdrenoGLES: QUALCOMM build : 35556ba, I9ca166462c
Build Date : 08/07/19
OpenGL ES Shader Compiler Version: EV031.27.02.00
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
Build Config : S P 8.0.8 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/: Successfully load libgui-plugin.so, this=0x757b838050
D/OnePlusJankManager: Chor uploadMDM JANK_TYPE_ONCE mViewTitle = at.fabian.zeiterfassungtest/at.fabian.zeiterfassungtest.MainActivity--- jank level = 1
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@ebd19a6[MainActivity]
D/MainActivity: Onclick für den Save Button
W/System.err: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2266)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2015)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:768)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:385)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:323)
at java.sql.DriverManager.getConnection(DriverManager.java:580)
at java.sql.DriverManager.getConnection(DriverManager.java:218)
at at.fabian.zeiterfassungtest.MainActivity.testDB(MainActivity.java:158)
at at.fabian.zeiterfassungtest.MainActivity$3.onClick(MainActivity.java:142)
at android.view.View.performClick(View.java:7201)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27562)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:188)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:299)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2187)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2220)
W/System.err: ... 23 more
D/OnePlusJankManager: Chor uploadMDM JANK_TYPE_ONCE mViewTitle = at.fabian.zeiterfassungtest/at.fabian.zeiterfassungtest.MainActivity--- jank level = 1
V/ViewRootImpl: The specified message queue synchronization barrier token has not been posted or has already been removed
W/Choreographer: Already have a pending vsync event. There should only be one at a time.
I have no idea what is wrong with my connection to the database and if I dont click the save button there is also an error message but the app does start.
08/25 15:06:17: Launching 'app' on OnePlus ONEPLUS A6013.
$ adb shell am start -n "at.fabian.zeiterfassungtest/at.fabian.zeiterfassungtest.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 19942 on device 'oneplus-oneplus_a6013-b41d8bd2'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
I/iterfassungtes: [GL_OOM] ClampGrowthLimit 268435456
V/Font: Change font:1
V/Font: Default family:android.graphics.Typeface@391c1c1d
E/Perf: Fail to get file list at.fabian.zeiterfassungtest
getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
W/iterfassungtes: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/iterfassungtes: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
V/ViewRootImpl: The specified message queue synchronization barrier token has not been posted or has already been removed
I/AdrenoGLES: QUALCOMM build : 35556ba, I9ca166462c
Build Date : 08/07/19
OpenGL ES Shader Compiler Version: EV031.27.02.00
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
Build Config : S P 8.0.8 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/: Successfully load libgui-plugin.so, this=0x757b838050
D/OnePlusJankManager: Chor uploadMDM JANK_TYPE_ONCE mViewTitle = at.fabian.zeiterfassungtest/at.fabian.zeiterfassungtest.MainActivity--- jank level = 1
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@ebd19a6[MainActivity]
If someone could help me with this problem, I would be very thankful.
User contributions licensed under CC BY-SA 3.0