لدي تطبيق يقوم بمحاولة بإنشاء اتصال بقواعد البيانات mysql ويظهر الخطأ التالي:
I/System.out: Error ************************java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
at com.example.testjdbc.MainActivity.getUserList(MainActivity.java:54)
W/System.err: at com.example.testjdbc.MainActivity.showData(MainActivity.java:73)
W/System.err: at com.example.testjdbc.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:7136)
W/System.err: at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
الكود:
privateConnection getConnection(){Connection con;try{Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/testandroid?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT","root","");return con;}catch(Exception e){System.out.println("Error :"+e);return null;}}privateArrayList<User> getUserList(){ArrayList<User> userList =newArrayList<>();Connection connection = getConnection();String query ="Select pseudo FROM `users` where userID="+1;Statement st;ResultSet rs;try{
st = connection.createStatement();
rs = st.executeQuery(query);User user ;while(rs.next()){
user =newUser(rs.getString("pseudo"));
userList.add(user);}}catch(Exception e){
e.printStackTrace();}return userList;}privatevoid showData(){ArrayList<User>list= getUserList();for(int i=0; i<list.size(); i++){
test.setText(list.get(i).getPseudo());
test.setText("gn");}}
السؤال
احمد صوالحة
لدي تطبيق يقوم بمحاولة بإنشاء اتصال بقواعد البيانات mysql ويظهر الخطأ التالي:
I/System.out: Error ************************java.sql.SQLNonTransientConnectionException: Could not create connection to database server. W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference at com.example.testjdbc.MainActivity.getUserList(MainActivity.java:54) W/System.err: at com.example.testjdbc.MainActivity.showData(MainActivity.java:73) W/System.err: at com.example.testjdbc.MainActivity.onCreate(MainActivity.java:24) at android.app.Activity.performCreate(Activity.java:7136) W/System.err: at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
الكود:
1 جواب على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.