java - connecting to a database through JDBC -
this question has answer here:
- connect java mysql database 11 answers
i trying follow tutorial in book on connecting program database jdbc. confused on first block of code doing in class. whan run code error saying java.sql.sqlexception: no suitable driver found jdbc:mysql://localhost:8889/book_store
and code throwing exception in first block inside class. need add sort of dependency or library project?
as can tell first attempt @ using db...
package com.apress.books.dao; import com.apress.books.model.author; import com.apress.books.model.book; import com.apress.books.model.category; import java.sql.*; import java.util.arraylist; import java.util.list; public class bookdaoimpl implements bookdao { static { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception ex) { } } private connection getconnection() throws sqlexception { return drivermanager.getconnection("jdbc:mysql://localhost:8889/book_store", "root", "password"); } private void closeconnection(connection connection) { if (connection == null) return; try { connection.close(); } catch (sqlexception ex) { } } @override public void insert(book book) { } @override public void update(book book) { } @override public void delete(long bookid) { } }
in essence classnotfoundexception. first catch clause empty , thus, although exception being caught, not acting upon it. in least, this:
static { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception ex) { throw ex; } }
this see better causing troubles.
moving on solution:
if using maven, need add following dependency in pom.xml:
<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.35</version> </dependency>
and if want, change version 1 found here.
otherwise, need download jar same link , include in project dependencies.
Comments
Post a Comment