java - how to load mysql jar in shell script and connect to MYSQL DB -


i have written small shell script invoke java class , inturn invokes mysql db , returns resultset.

my shell scipt:

cp="." cp=$cp:/home/lib/mysql-connector-java-5.1.12-bin.jar export cp java -cp $cp com.util.hello   

and java code follows,

hello.java:

public static void main(string[] str) {  connection conn = null;           try           {               string username = "root";               string password = "root";               string url = "jdbc:mysql://localhost:3306/test";               class.forname ("com.mysql.jdbc.driver").newinstance ();               conn = drivermanager.getconnection (url, username, password);               system.out.println ("database connection established");                  statement s = conn.createstatement ();                 s.executequery ("select * users");                 resultset rs = s.getresultset ();                 int count = 0;                 while (rs.next ())                 {                                            system.out.println (count + " rows retrieved");                     count++;                     }                     rs.close ();                     s.close ();          }         catch (exception e) {              system.err.println ("cannot connect database server")         }  } 

when execute shell script linux server getting below error,

cannot connect database server have printed in java class

system.err.println ("cannot connect database server") 

can me in how load mysql jar shells script ?

and how use while running java class?

if run class war file working.

thanks in advance.

you need mysql-connector-java-5.1.12-bin.jar in /jre/lib/ext folder in java jre folder think easy way run java database program


Comments