简体   繁体   中英

Java DB (Embedded) Connect using

I have created Database named emb4 in Java using NetBeans IDE, with one table and I am trying to create application with embedded DB. I have created my embedded driver and I am using this code :

>     /*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package embededbaza4;
> 
> import java.sql.Connection; import java.sql.DriverManager; import
> java.sql.ResultSet; import java.sql.Statement;
> 
> 
> /**  *  * @author Admin  */ public class EmbededBaza4 {
> 
>     
>      private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
>      
>     /**
>      * @param args the command line arguments
>      */
>     public static void main(String[] args) {
>         // TODO code application logic here
>         
>         try
>         {
>             Class.forName(driver).newInstance();
>             Connection conn = null;
>             conn = DriverManager.getConnection("jdbc:derby:emb4;create=true");
>             
>         Statement st = conn.createStatement();
>         String sql = "SELECT * FROM TEKSTOVI";
>         ResultSet setRezultata = st.executeQuery(sql);
>         
>        while(setRezultata.next())
>        {
>          System.out.println(setRezultata.getString("txt"));
>        }
>        
>        
>         }
>         catch(Exception exc)
>         {
>             System.err.println("Unable to load the embedded driver."); 
>              exc.printStackTrace(System.err); 
>             System.exit(0); 
>         }
>     } }

But it won't connect to database I have created, rather it's creating a new database and there isn't a table I have created . How can I resolve this problem?

Try using the absolute path to the database in the connection URL. Eg jdbc:derby:/path/to/emb4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM