Reputation:
I was trying to access my database from a java program and created one table to see if it works.
The program compiled perfectly fine, yet the table doesn't appear in the program output. What's wrong?
package db_cliinic;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DB_Cliinic {
public static void main(String[] args) {
Connection con = null;
Statement stat = null;
ResultSet res = null;
String q = "select * from DB.EMPLOYEE";
try {
con = DriverManager.getConnection("jdbc:derby://localhost1527/VetClinic", "DB", "123456");
stat = con.createStatement();
res = stat.executeQuery(q);
while (res.next()) {
char id = (char) res.getCharacterStream("EMP_ID").read();
String name = res.getString("NAME");
char phone =(char) res.getCharacterStream("PHONE_NUMBER").read();
String Edu = res.getString("EDUCATION_LEVEL");
double sal = res.getDouble("SALARY");
String role = res.getString("ROLE");
char sex = (char) res.getCharacterStream("SEX").read();
String addr = res.getString("ADDRESS");
char crn = (char) res.getCharacterStream("ECRN").read();
char super_id = (char) res.getCharacterStream("SUPER_ID").read();
System.out.println(" "+id+" "+name+" "+phone+" "+Edu+" "+sal+" "+role+" "+sex+" "+addr+" "+crn+super_id);
}
}
catch (SQLException e){
} catch (IOException ex) {
Logger.getLogger(DB_clinic.class.getName()).log(Level.SEVERE, null, ex); } } }
Upvotes: 1
Views: 81
Reputation: 108
Try with the following code:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DB_Cliinic {
public static void main(String[] args) {
Connection con = null;
Statement stat = null;
ResultSet res = null;
String q = "select * from EMPLOYEE";
try {
con = DriverManager.getConnection("jdbc:derby://localhost:1527/VetClinic;");
stat = con.createStatement();
res = stat.executeQuery(q);
while (res.next()) {
char id = (char) res.getCharacterStream("EMP_ID").read();
String name = res.getString("NAME");
char phone =(char) res.getCharacterStream("PHONE_NUMBER").read();
String Edu = res.getString("EDUCATION_LEVEL");
/*TODO: uncomment the block below*/
/*double sal = res.getDouble("SALARY");
String role = res.getString("ROLE");
char sex = (char) res.getCharacterStream("SEX").read();
String addr = res.getString("ADDRESS");
char crn = (char) res.getCharacterStream("ECRN").read();
char super_id = (char) res.getCharacterStream("SUPER_ID").read();*/
System.out.println(" "+id+" "+name+" "+phone+" "+Edu);
}
}
catch (SQLException e){
System.out.println(e.getMessage());
} catch (IOException ex) {
Logger.getLogger(DB_Cliinic.class.getName()).log(Level.SEVERE, null, ex); } } }
Try to add one stage of debugging as I did with: System.out.println(e.getMessage());
you will see the error.
The error is caused by a connection problem to the database.
Correction of the connection problem
Your database name is VetClinic
and your table is EMPLOYEE
Interactive Derby command to create your database
connect 'jdbc:derby:VetClinic;create=true';
Create the table and insert data
create table EMPLOYEE(EMP_ID char(10) primary key, NAME varchar(50), PHONE_NUMBER char(10), EDUCATION_LEVEL varchar(50), ...)
;
insert into EMPLOYEE values ('id-1', 'name1', '00001', "Level10"), ('id-2', 'name2', '00002', 'Level11'), ...;
jdbc:derby://localhost:1527/VetClinic;
Before you execute the program start the Derby server, the message must look like
Please make sure that Apache Derby Network Server started and is ready to accept connections on port 1527
Upvotes: 2
Reputation: 308
you most likely get a SQLException
but it's catch block is empty. Try to log it as well and see what it says.
Your connection URL looks suspicious - I assume you meant to say "localhost:1527" rather than "localhost1527"?!
Upvotes: 0