public class MainActivity extends AppCompatActivity {
String TAG=MainActivity.class.getCanonicalName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void a_one(View view){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hao?serverTimezone=UTC","root","1111");
String sql="select * from two";
ResultSet rs= (ResultSet) conn.prepareStatement(sql);
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"
+rs.getInt(2)+"\t" +rs.getInt(3)+"\t"+rs.getInt(4)+"\t"
);
}
Log.i(TAG,"ssssssssssssssssssssss");
}catch(Exception e){
e.printStackTrace();
}
}
}
然后下面是错误
W/System.err: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at java.lang.Class.forName(Class.java:378)
W/System.err: at com.example.abcd.a_jdbclianjie.MainActivity.a_one(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
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)
W/System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mysql.cj.jdbc.Driver" on path: DexPathList[[zip file "/data/app/com.example.abcd.a_jdbclianjie-a23PY8DiYHeGuU0KvG9Kbw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.abcd.a_jdbclianjie-a23PY8DiYHeGuU0KvG9Kbw==/lib/x86, /system/lib]]
W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 17 more
这是为什么呢?为什么驱动会报错呢?
new com.mysql.cj.jdbc.Driver();
或者
new com.mysql.jdbc.Driver();
并确保引用了
mysql-connector-java
感谢感谢,我添加了new之后能往下运行了,可是我在新的Java class里面测试com.mysql.cj.jdbc.Driver时能连接上的啊,为什么到安卓这个就不好使了?
还有一个问题。我调用这个方法后显示加载失败,是还是连接有问题还是怎么回事啊?
public void jdbclian(){
try {
Class.forName("new com.mysql.cj.jdbc.Driver");
System.out.println("加载成功");
} catch (ClassNotFoundException e) {
System.out.println("加载失败");
}
}
--------------------------------------------------------------------------------------------------------
错误:
I/System.out: 加载失败
08-05 05:34:11.243 21157-21189/com.example.abcd.wodetiana W/System.err: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/hao?serverTimezone=UTC
08-05 05:34:11.257 21157-21189/com.example.abcd.wodetiana W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:594)
--------------------------------------------------------------------------------------------------
错误说找不到适用于jdbc的驱动程序是我这个mysql://localhost:3306/hao?serverTimezone=UTC 写错了吗?
@haooahao: 认真看回答。看你应该是个新手,不知道你开发情况,但首先请确保引用mysql驱动包,eclipse的话libs目录有没有mysql-connector-java-*.jar文件,Android studio的话build.gradle有没有compile group: 'mysql', name: 'mysql-connector-java' ,没有这种写法Class.forName("new com.mysql.cj.jdbc.Driver"); ,使用new com.mysql.jdbc.Driver(); 或者 new com.mysql.cj.jdbc.Driver();替换,取决于你用的mysql驱动包的版本,我估计你Android手机没有安装mysql服务,localhost地址是不对的。