Database and java.sql.SQLException Questions
- From: "zhah99 via JavaKB.com" <u19084@uwe>
- Date: Wed, 26 Apr 2006 03:44:18 GMT
Hello everyone!! I am back for some more much need advice and assistance! I
am creating a database for the first time. I am changing some programs I
recently wrote to work with this database that have to do with Salesmen. I
am getting the below error when I compile CreateDatabase.java. In that I am
creating the database SalesDatabase and then creating 5 tables (Users, SType,
Salesman, Sales, Product). The first thing I do is create those tables and
then insert informtion into the SType table and Product table. SType is the
SalesType of a salesman, meaning Entry, Junior or Senior that also has the
bonus in there as another field (.05, .10, .15) and then product has product
1, 2, 3, 4, and 5 with the corresponding price of each. After I do this I
need to read in a text file (SalesInformation.txt) and calculate the sales
based on the information in the database. So here are my questions:
(1) What is the below error telling me, have I not set the database up
correctly?
(2) Am I on the right track with what I have below, should I be reading the
text file as I am or should I change it to be its own method (anything you
tell me would be apprecaited!!)?
(3) Also, other than it not compiling...lol...I am unsure of the salestype
part with element 6 on line 124. I read in ENTRY, JUNIOR or SENIOR from the
salesinformation.txt file so how do I relate that with retrieving the bonus
number from the database?
Ok, I think that is enough questions for now. If anyone needs any additional
information as to what I am trying to do, just let me know and I would be
happy to fill you in!! I appreciate any hints or tips you may be able to
give and I look forward to getting this thing working :)!! Thanks everyone!
Error:
[code]
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not
fou
nd and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.
java:3
23)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at CreateDatabase.main(CreateDatabase.java:31)
[/code]
SalesInformation.txt
[code]
Jones&2&5&3&0&8&SENIOR
Smith&6&0&2&0&5&JUNIOR
Douglas&1&0&7&5&3&ENTRY
Hollins&9&6&3&7&15&ENTRY
Smythe&0&0&0&0&0&SENIOR
Billings&0&7&12&15&2&JUNIOR
Kozak&3&5&8&22&0&ENTRY
[/code]
CreateDatabase:
[code]
import java.io.*;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.*;
import sun.jdbc.odbc.*;
public class CreateDatabase
{
static String salesmanname;
int salesmanID;
static double saleslevel;
double salesbonus;
static double sales = 0.0;
double productprice;
static String prod1price;
static String prod2price;
static String prod3price;
static String prod4price;
static String prod5price;
public static void main(String args[])
{
String line;
try
{
new JdbcOdbcDriver();
String url = "jdbc:odbc:SalesDatabase";
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE Users (UserName VARCHAR(25)," +
"UserPassword VARCHAR(25))");
stmt.executeUpdate("CREATE TABLE Stype (SalesType VARCHAR(25)," +
"SalesBonus CURRENCY)");
stmt.executeUpdate("CREATE TABLE Salesman (SalesmanName VARCHAR(25)," +
"SalesmanID INTEGER(10), SalesType VARCHAR(25))");
stmt.executeUpdate("CREATE TABLE Sales (SalesmanID INTEGER(10)," +
"TotalSales CURRENCY)");
stmt.executeUpdate("CREATE TABLE Product (ProductNumber INTEGER(1)," +
"ProductPrice VARCHAR(5))");
String insert1 = "INSERT INTO Stype VALUES('Entry', .05)";
String insert2 = "INSERT INTO Stype VALUES('Junior', .10)";
String insert3 = "INSERT INTO Stype VALUES('Senior', .15)";
stmt.executeUpdate(insert1);
stmt.executeUpdate(insert2);
stmt.executeUpdate(insert3);
String insert4 = "INSERT INTO Product VALUES(1, 2.98)";
String insert5 = "INSERT INTO Product VALUES(2, 4.50)";
String insert6 = "INSERT INTO Product VALUES(3, 9.98)";
String insert7 = "INSERT INTO Product VALUES(4, 4.49)";
String insert8 = "INSERT INTO Product VALUES(5, 6.87)";
stmt.executeUpdate(insert4);
stmt.executeUpdate(insert5);
stmt.executeUpdate(insert6);
stmt.executeUpdate(insert7);
stmt.executeUpdate(insert8);
String insert9 = "INSERT INTO Salesman VALUES(" +
salesmanname + "," + "''" + saleslevel +")";
stmt.executeUpdate(insert9);
String insert10 = "INSERT INTO Sales VALUES(" +
sales +")";
stmt.executeUpdate(insert10);
String query1 = "SELECT ProductPrice FROM Product WHERE ProductNumber =
1";
ResultSet rs1 = stmt.executeQuery(query1);
while(rs1.next())
prod1price = rs1.getString("ProductPrice");
String query2 = "SELECT ProductPrice FROM Product WHERE ProductNumber =
2";
ResultSet rs2 = stmt.executeQuery(query2);
while(rs2.next())
prod2price = rs2.getString("ProductPrice");
String query3 = "SELECT ProductPrice FROM Product WHERE ProductNumber =
3";
ResultSet rs3 = stmt.executeQuery(query3);
while(rs3.next())
prod3price = rs3.getString("ProductPrice");
String query4 = "SELECT ProductPrice FROM Product WHERE ProductNumber =
4";
ResultSet rs4 = stmt.executeQuery(query4);
while(rs4.next())
prod4price = rs4.getString("ProductPrice");
String query5 = "SELECT ProductPrice FROM Product WHERE ProductNumber =
5";
ResultSet rs5 = stmt.executeQuery(query5);
while(rs5.next())
prod5price = rs5.getString("ProductPrice");
FileReader file = new FileReader("SalesInformation.txt");
BufferedReader buff = new BufferedReader(file);
while ((line = buff.readLine())!= null)
{
String[] elements = line.split("&");
salesmanname = elements [0];
double sales1 = 0.0;
double sales2 = 0.0;
double sales3 = 0.0;
double sales4 = 0.0;
double sales5 = 0.0;
sales1 = Double.parseDouble(elements[1]) * Double.parseDouble(prod1price)
;
sales2 = Double.parseDouble(elements[2]) * Double.parseDouble(prod2price)
;
sales3 = Double.parseDouble(elements[3]) * Double.parseDouble(prod3price)
;
sales4 = Double.parseDouble(elements[4]) * Double.parseDouble(prod4price)
;
sales5 = Double.parseDouble(elements[5]) * Double.parseDouble(prod5price)
;
saleslevel = (elements[6]); // I am a little unsure of how to use what
is in the database
// and apply it here to calculate the saleslevel for a salesman..?
?
try
{
sales = (sales1 + sales2 + sales3 + sales4 + sales5);
if( sales == 0.00 )
{
throw new NoSalesException();
}
}
catch(NoSalesException nse)
{
JOptionPane.showMessageDialog(null, salesmanname + " has no Sales!!",
"Sales Equals Zero",
JOptionPane.WARNING_MESSAGE);
}
sales = sales + (sales * saleslevel);
}
buff.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
[/code]
--
Message posted via http://www.javakb.com
.
- Follow-Ups:
- Re: Database and java.sql.SQLException Questions
- From: Jon Martin Solaas
- Re: Database and java.sql.SQLException Questions
- From: Bjorn Abelli
- Re: Database and java.sql.SQLException Questions
- Prev by Date: Re: MSAccess get Query SQL source possible
- Next by Date: Intersystems Cache' - Anyone use it?
- Previous by thread: SQL CREATE TABLE ERROR
- Next by thread: Re: Database and java.sql.SQLException Questions
- Index(es):
Relevant Pages
|
|