java - I can't connect to a database - program doesn't know where JAR file is -


i trying copy example chapter 23 of cay s. horstmann's big java, can find online pdf file in case if need reference: http://bcs.wiley.com/he-bcs/books?action=chapter&bcsid=7872&itemid=1118431111&chapterid=87075

my program supposed connect apache derby database using java utility tool in shell window, doesn't work. supposed entering shell window fix problem?

my folder structure looks in intellij:

my intellij folder structure

this code:

package chapter23relationaldatabases.database;  import java.io.*; import java.sql.*;  public class testdb {     public static void main(string[] args) throws exception {         if (args.length == 0) {             system.out.println(                     "usage: java -classpath driver_class_path"                     + file.pathseparator                     + ". testdb propertiesfile");             return;         }          simpledatasource.init(args[0]);          connection conn = simpledatasource.getconnection();         try {             statement stat = conn.createstatement();              stat.execute("create table test (name varchar(20))");             stat.execute("insert test values ('romeo')");              resultset result = stat.executequery("select * test");             result.next();             system.out.println(result.getstring("name"));              stat.execute("drop table test");         } {             conn.close();         }     } } 

package chapter23relationaldatabases.database;  import java.io.*; import java.sql.*; import java.util.properties;  public class simpledatasource {     private static string url;     private static string username;     private static string password;      /**      * initializes data source.      * @param filename name of property file contains      *                 database driver, url, username, , password      */     public static void init(string filename) throws ioexception, classnotfoundexception {         properties props = new properties();         fileinputstream in = new fileinputstream(filename);         props.load(in);          string driver = props.getproperty("jdbc.driver");         url = props.getproperty("jdbc.url");         username = props.getproperty("jdbc.username");         if (username == null)             username = "";         password = props.getproperty("jdbc.password");         if (password == null)             password = "";         if (driver != null)             class.forname(driver);     }      /**      * gets connection database.      * @return database connection      */     public static connection getconnection() throws sqlexception {         return drivermanager.getconnection(url, username, password);     } } 

this contents of database.properties file:

jdbc.url=jdbc:derby:bigjavadb;create=true 

this error stack when try run program:

 exception in thread "main" java.sql.sqlexception: no suitable driver found jdbc:derby:bigjavadb;create=true  @ java.sql.drivermanager.getconnection(drivermanager.java:689)  @ java.sql.drivermanager.getconnection(drivermanager.java:247)  @ chapter23relationaldatabases.database.simpledatasource.getconnection(simpledatasource.java:42)  @ chapter23relationaldatabases.database.testdb.main(testdb.java:20) 

why enter in "shell window" when using intellij? need add derby jars class path, if use maven can add this:

    <!-- apache derby - embedded database -->     <dependency>         <groupid>org.apache.derby</groupid>         <artifactid>derby</artifactid>         <version>10.10.1.1</version>     </dependency>     <dependency>         <groupid>org.apache.derby</groupid>         <artifactid>derbyclient</artifactid>         <version>10.10.1.1</version>     </dependency>      <!-- mysql connector -->     <dependency>         <groupid>mysql</groupid>         <artifactid>mysql-connector-java</artifactid>         <version>5.1.28</version>     </dependency> 

otherwise can search these here http://mvnrepository.com/artifact/org.apache.derby/derby download jars , add them class path manually beginners do. can add jars classpath on command line -cp flag use command line java learn basic programming concepts. other wise use build tool maven. think should maybe checkout tutorial videos adding jars classpath intellij/eclipse/netbeans. learn how use maven, highly recommend beginner.


Comments