8 Replies - 1598 Views - Last Post: 10 November 2009 - 02:56 AM Rate Topic: -----

#1 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-May 09

Database connectivity in Java

Posted 28 June 2009 - 10:07 AM

Hi,
I have a question for u, i need to read from a coin table and connect my access database to the following program:
import java.util.Scanner;

public class Coin {
	
	static String coins[] = { "pennies", "nickels", "dimes", "quarters",
			"half-dollars", "dollars" };
	
	static int values[] = { 1, 5, 10, 25, 50, 100 };

	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);// standard input from console
		int amount = 0; // entered value of coins

		for (int i = 0; i < coins.length; i++) {
			System.out.print("Please enter number of " + coins[i] + ":");
			amount += values[i] * in.nextInt();
		}

		System.out.println("The total sum of change you entered is: " + amount
				+ " cents.");
	}

}




how would i do this?

never did this before trying to read up on it any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Database connectivity in Java

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Database connectivity in Java

Posted 28 June 2009 - 10:18 AM

@mb1980, assuming you dont have any previous knowledge in this Java/Access connectivity issue here are some resources that show you how to connect your Java program to an Access database and share data:

http://www.dreaminco...?showentry=1114 This one is from D.I.C. written by Martyr2

here are some other articles:

http://java.sun.com/...connecting.html

http://www.wilsonmar.com/1jdbc.htm

http://www.developer...icle.php/942641

Hope this helps, good luck :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Database connectivity in Java

Posted 28 June 2009 - 10:37 AM

@mb1980: It's not very nice to hijack someone's thread with your own question, and it also means it isn't going to get the kind of attention it needs so I split it into it's own thread :)
Was This Post Helpful? 0
  • +
  • -

#4 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-May 09

Re: Database connectivity in Java

Posted 29 June 2009 - 06:24 AM

Ok thank you i apologize for that...... .

ok i am running this in eclipse why does it give me the following error?

Editor does not contain a main type...

import java.sql.*;

class DatabaseTest


	{
	
	public static void main(String[] args)


		{


			try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// set this to a MS Access DB you have on your machine
			String filename = "C:/Documents and Settings/Markus/Desktop/example.mdb";
			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
			database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
			// now we can get the connection from the DriverManager
			Connection con = DriverManager.getConnection( database ,"",""); 
		}



			catch (Exception e) {
			System.out.println("Error: " + e);
		}

	}

}


Was This Post Helpful? 0
  • +
  • -

#5 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-May 09

Re: Database connectivity in Java

Posted 29 June 2009 - 07:02 AM

ok it seems to work with this now how do i get my coin program integrated in this?

import java.sql.*;
class Test


	{
	public static void main(String[] args)


		{


			try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// set this to a MS Access DB you have on your machine
			String filename = "C:/Documents and Settings/Markus/Desktop/Database Files/example.mdb";
			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
			database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
			// now we can get the connection from the DriverManager
			Connection con = DriverManager.getConnection( database ,"",""); 
		}



			catch (Exception e) {
			System.out.println("Error: " + e);
		}

	}

}


Was This Post Helpful? 0
  • +
  • -

#7 koki  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 02-January 09

Re: Database connectivity in Java

Posted 29 June 2009 - 07:22 AM

you can connect to database using many drivers ... i usually use that one.
public DataBase()
	{
		try
		{
			DriverManager.registerDriver(new JdbcOdbcDriver());
			Connection c=DriverManager.getConnection("jdbc:odbc:myjava","","");
			Statement st=c.createStatement();
			String s=new String("select * from java");
			ResultSet rs=st.executeQuery(s);
			while(rs.next())
			{
				int id=rs.getInt(1);
				String name=rs.getString(2);
				System.out.println(id+":"+name);
				
			}
		}
		
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}

and don't forget to import these libraries
import sun.jdbc.odbc.*;
import java.sql.*;


Was This Post Helpful? 1
  • +
  • -

#8 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-May 09

Re: Database connectivity in Java

Posted 29 June 2009 - 08:13 AM

the code u gave me did not work ???

I am just playing around to understand what i am doing here, this is what i did:

import java.sql.*;
class Test


	{
	public static void main(String[] args)


		{


			try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// set this to a MS Access DB you have on your machine
			String filename = "C:/Documents and Settings/Markus/Desktop/Database Files/example.mdb";
			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
			database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
			// now we can get the connection from the DriverManager
			Connection con = DriverManager.getConnection( database ,"",""); 
			Statement s = con.createStatement();
			s.execute("create table Coin ( coin_name char(20))"); // create a table
			s.execute("insert into Coin values('pennies')");
			s.execute("select coin_name from Coin"); // select the data from the table
			ResultSet rs = s.getResultSet(); // get any ResultS
			if (rs != null) // if rs == null, then there is no ResultSet to view
			while ( rs.next() ) // this will step through our data row-by-row

			{
				/* the next line will get the first column in our current row's ResultSet 
				as a String ( getString( columnNumber) ) and output it to the screen */ 
				System.out.println("Data from column_name: " + rs.getString(1) );
			}

		}



			catch (Exception e) {
			System.out.println("Error: " + e);
		}

	}




i need to build a table coin in access and the only output i get now is :
Data from column_name: pennies :blink:

this should be the output of the entire program once it is done:

The program will the prompt the user to enter change and will calculate the sum by looking up the values from the data stored in the database. The program run should look like:

Please enter number of pennies: ____
Please enter number of nickels: ____
Please enter number of dimes: ____
Please enter number of quarters: ____
Please enter number of half-dollars: ____
Please enter number of dollars: ____

The total sum of change you entered is: ____
Was This Post Helpful? 0
  • +
  • -

#9 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-May 09

Re: Database connectivity in Java

Posted 29 June 2009 - 08:27 AM

I take that back your program IS working i am just overworked this is what i did:

import java.sql.*;
class Test


	{
	public static void main(String[] args)


		{


			try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// set this to a MS Access DB you have on your machine
			String filename = "C:/Documents and Settings/Markus/Desktop/Database Files/example.mdb";
			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
			database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
			// now we can get the connection from the DriverManager
			Connection con = DriverManager.getConnection( database ,"",""); 
			Statement st=con.createStatement();
			String s=new String("select * from Coin");
			ResultSet rs=st.executeQuery(s);
			while(rs.next())
			{
				int coin_value=rs.getInt(1);
				String name=rs.getString(2);
				System.out.println(name+":"+coin_value);
				
			}

		}



			catch (Exception e) {
			System.out.println("Error: " + e);
		}

	}





you are giving me great ideas keep it coming heheh

Thank u mate
Was This Post Helpful? 0
  • +
  • -

#10 JBeginer1987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-November 09

Re: Database connectivity in Java

Posted 10 November 2009 - 02:56 AM

View Postkoki, on 29 Jun, 2009 - 06:22 AM, said:

you can connect to database using many drivers ... i usually use that one.
public DataBase()
	{
		try
		{
			DriverManager.registerDriver(new JdbcOdbcDriver());
			Connection c=DriverManager.getConnection("jdbc:odbc:myjava","","");
			Statement st=c.createStatement();
			String s=new String("select * from java");
			ResultSet rs=st.executeQuery(s);
			while(rs.next())
			{
				int id=rs.getInt(1);
				String name=rs.getString(2);
				System.out.println(id+":"+name);
				
			}
		}
		
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}

and don't forget to import these libraries
import sun.jdbc.odbc.*;
import java.sql.*;



Hi , i know im intruding here but I kinda don't get it. Do I have to use the snippet u posted in the main method by calling it as
DataBase();


thks, your snippet helped me alot...but still struggling!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1