Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use common-dbcp package's problem

 
gao zhixin
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a fragment of code:

BasicDataSource ds=new BasicDataSource();
Class.forName("com.mysql.jdbc.Driver");
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306");
ds.setUsername("root");
ds.setPassword("gaopass");
Connection conn=ds.getConnection();
PreparedStatement stmt=conn.prepareStatement("insert into blog values(?,?,?)");
stmt.setString(1,"asd");
stmt.setString(2,"dsa");
stmt.setString(3,"aaa");
stmt.execute();
stmt.close();
conn.close();



and these are Exception throws from console:

Exception in thread "main" org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost:3306'
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at DT.main(DT.java:17)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
... 2 more

why ???
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most probably, the driver java is not in the classpath being used at execution time.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps the problem is that you didn't specify the name of the database in your JDBC URL.
 
gao zhixin
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem solved, I forgot to tell the Driver the database I use. It's a shame to ask the problem like this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic