Tomcat起動エラーNo suitable driver found for jdbc:mysql://localhost:3306……
2282 ワード
問題原因:この異常原因は多いかもしれません.
1.符号化問題2.クラスドライバをロードしていません:データベースの接続方法を変えて、これを忘れました:Class.forName(driverclass);
質問:
mysqlを使用する場合、駆動文字列はClassです.forName(com.mysql.jdbc.Driver); buildpathでmysqlをインポートするかどうかjdbcドライバ、バージョンがjarパケットの位置に一致するかどうか
標準的なコードを添付します.プレゼンテーションの内容は気にしないでください.
1.符号化問題2.クラスドライバをロードしていません:データベースの接続方法を変えて、これを忘れました:Class.forName(driverclass);
質問:
// url ,
private static final String url="jdbc:mysql://localhost:3306/ ?useUnicode=true&characterEncoding=utf-8";
mysqlを使用する場合、駆動文字列はClassです.forName(com.mysql.jdbc.Driver); buildpathでmysqlをインポートするかどうかjdbcドライバ、バージョンがjarパケットの位置に一致するかどうか
標準的なコードを添付します.プレゼンテーションの内容は気にしないでください.
package com.zch.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @ClassName: Example03.java
* @Description:
* @author
* @version V1.0
* @Date 2019 2 13 10:33:15
*
*/
public class Example03 {
public static void main(String[] args) {
Connection con=null;
Statement st=null;
ResultSet rs=null;
try {
//1.
Class.forName("com.mysql.jdbc.Driver");
//2.
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root","201314");
//3. sql ,
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//4. sql
String sql="select * from users";
rs=st.executeQuery(sql);
//5.
//
rs.absolute(2);
System.out.println(" "+rs.getString(2));
//
rs.beforeFirst();//
rs.next(); //
System.out.println(" :"+rs.getString("name"));
//
rs.afterLast();//
rs.previous();//
System.out.println(" :"+rs.getString(2));
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException(e);
}finally {
//
if(rs!=null) {
try {
rs.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
rs=null;
}
if(st!=null) {
try {
st.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
st=null;
}
if(con!=null) {
try {
con.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
con=null;
}
}
}
}