f



query ms access database from the user

At college i have been given this piece of untidy code because it mixes
a gui interface with the console, i prefare one or the other, but its
not my code, i understand what it does.
It opens the database CarDB by using the java odbc (Open Database
Connectivity ) driver
and searches for the row make  with nissan and displays to the screen
as text in a gui text box the columns Registration, Model, Year, Price
in the same row as the nissan .
I would like the programme to ask the user which car their want to
search for .
Unfortunately i do not learn at college or have taught myself sql.
If i am understanding proberly the line
ResultSet rec = st.executeQuery( "SELECT Registration, Model, Year,
Price   FROM Table1 where Make='nissan'");
is where the query starts, but its hardcoded in, i presume i need to
have a line similar to
ResultSet rec = st.executeQuery( "SELECT Registration, Model, Year,
Price   FROM Table1 request Make=' '") or something similar, am i along
the correct lines?

here is the code..

import java.sql.*;
//import javax.swing.JOptionPane;
//import javax.swing.JTextArea;
import java.io.*;
public class Car2 {
	public static void main(String args[]){
		Connection connection;
   		Statement st;
   		String out="";

		JTextArea display=new JTextArea();
   		try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   		connection= DriverManager.getConnection("jdbc:odbc:CarDB","","");
   		st = connection.createStatement();
				statement = connection.createStatement();

              ResultSet rec = st.executeQuery( "SELECT Registration,
Model, Year, Price   FROM Table1 where Make='nissan'");
   		while (rec.next()) {
                        out=" nissan: " +
rec.getString("Registration")+"\t"+
         rec.getString("Model")+"\t"+ rec.getString("Year") +
			"\t"+ rec.getString("Price");
    			display.setText(out);
   			JOptionPane.showMessageDialog(null,display);
    	}
    	}catch (Exception e) {
   			e.printStackTrace(); 
  		}
  	}
  }


thanks in advance

0
adaviscg1 (83)
5/20/2006 9:35:05 AM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

2 Replies
994 Views

Similar Articles

[PageSpeed] 24

"geletine" wrote...

> If i am understanding proberly the line
> ResultSet rec = st.executeQuery( "SELECT Registration, Model, Year,
> Price   FROM Table1 where Make='nissan'");

That's where the query is executed, just as the method name proposes...
;-)

> is where the query starts, but its hardcoded in, i presume
> i need to have a line similar to
> ResultSet rec = st.executeQuery( "SELECT Registration, Model,
> Year, Price   FROM Table1 request Make=' '") or something similar,
> am i along the correct lines?

Not even close...

You have to get the "make" from the user *before* you make the call to the 
database.

When you have that, you can use a PreparedStatement to "insert" the value 
into the query.

String make = ... // received from user

....

PreparedStatement ps =
 connection.prepareStatement(
  "SELECT Registration, Model, Year, Price " +
  "FROM Table1 where Make = ? ");

ps.setString(1, make);

ResultSet rec = ps.executeQuery();

> thanks in advance

You're welcome.

/// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
0
bjorn_abelli (300)
5/20/2006 9:53:09 AM
Thank you very much , i converted that code into a console only app
which i think is alot cleaner .

here is the code which works

import java.sql.*;
import java.io.*;
public class Car2 {
	public static void main(String args[]){
		Connection connection;
   		Statement st;
   		String out="";
			String display="";
		  String  make = "";
			BufferedReader info = new BufferedReader ( new InputStreamReader (
System.in ) );

  		try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   		connection= DriverManager.getConnection("jdbc:odbc:CarDB","","");
   		st = connection.createStatement();

			System.out.println("Enter name of car");
			make = info.readLine();

	          PreparedStatement ps =
			 connection.prepareStatement(
  "SELECT Registration, Model, Year, Price " +
  "FROM Table1 where Make = ? ");


				ps.setString(1, make);


		ResultSet rec = ps.executeQuery();


   		while (rec.next()) {
                        out=" : " + rec.getString("Registration")+"\t"+

rec.getString("Model")+"\t"+ rec.getString("Year") +
			"\t"+ rec.getString("Price");

			display = out;
			System.out.println (display);
    			}
    	}catch (Exception e) {
   			e.printStackTrace(); 
  		}
  	}
  }

0
adaviscg1 (83)
5/20/2006 10:29:02 AM
Reply:

Similar Artilces:

Create MS/ACCESS database app for non-ms/access users
Is it possible to create an MS/ACCESS database application and package it for users who do not have MS/ACCESS loaded in their PCs? I was told that this is possible, but I don't know what software is needed. Do you know? If so, please answer this post. Thanks. SueB *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it! Per Sue Bricker: >Is it possible to create an MS/ACCESS database application and package >it for users who do not have MS/ACCESS loaded in their PCs? > >I was told that this is possible, but...

multiple users' simultaneous access to a MS Access database
When my end user clicks on the icon for her MS Access database (the icon is on her Novell NAL menu) she is successful in opening the database. However, when another staff member is using the database she receives the message that someone is using it and she cannot access it at that time. Any suggestions as to what I need to do to resolve this? I guess it would be a fix to allow multiple users simultaneous access to a MS Access database? Thanks in advance, Zuf Per zufie: >When my end user clicks on the icon for her MS Access database (the >icon is on her Novell NAL menu...

MS Access Database Query
Hi, first of all I have to say I'am new to MS Access, I'm used to database systems like Oracle or MySQL. I created a simple database consisting of a single table and a form to fill in new entrys and search for existing entrys. But the search function that Access generates as I created the proper button is way to simple, I need a function that enables to search in several columns. I saw a few quite simple examples where this was done by DAO, but this doesn't work with a .mdb database, does it? (Access allways replies 'Unknown user datatype' once it gets to the line: 'Dim...

Updating MS-Access Database query
hello i need to update my MS-Access database thru matlab. when using the following code using function "updates" it updates the database i.e .......whereClause = 'where month = ''Nov'''..... Here "Nov" is an entry in the database. Now when i assign a variable to the months..ie variable name "MNTH" could contain Jan /Feb etc,.....and i want to pass the variable "MNTH" ie .......whereClause = 'where month = ''MNTH'''..... it DOES NOT update the database and i receive the error .....error in horxcat......

Java with MS Access 2000 Database
Hi All I am trying to connect with MS Access database but faild. Please let me know what are the alternatives, so that I can connect to an Access Database. Thanks in Advance Zee What are you failing with? Make sure you have your data source/DB setup via control panel. The following pice of code shows how I connect to a ms access database called myDB. ------------------ // Load the driver class Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Define the data source for the driver String sourceURL = "jdbc:odbc:myDB"; // Create a connection thr...

Use ASP to read MS Access query that calls MS Access function
Hi: I have an MS Access query for a table called MyTable. One of my fields in the query is a custom field that looks like "MyField: = MyFunction(ID)" where ID is a field in MyTable. I have ASP code and I can use it to read the query OK when "MyField" is not part of the query. If I add in MyField and then run my ASP code to try to read all the query fields, I get an error message on my web browser which says something to the effect of "cannot find MyFunction." On the Access side, I have MyFunction set up in a module and use the keyword "Public" in fr...

Using ActiveX to query MS Access Database
Greetings Earthlings, I've been exploring different ways to query MS Access databases using ActiveX controls thru MATLAB. I use actxserver('access.application') to get a component that is populated with many interfaces and methods. My question is, is there documentation addressing how to use these methods and interfaces? I can't find any documentation describing what these methods and interfaces are and how to use them. I.e., what arguments do they accept, what is returned, how do I know which one to use to do which task? In particular, I need to direct matlab to ...

Track the User logged in the Shared MS Access Database
Hi, is there a way to know which user last logged in the shared Access database. My requirement is that I must be able to track the users that log in the database and incase its not working because one of the user has done something wrong that I must be able to track him/her. TIA -G. See: HOW TO: Check Who Logged into Database with Jet UserRoster in Access 2000 at: http://support.microsoft.com/?id=198755 You may find it easier to use your initialization code (e.g. AutoExec macro) to actually log the users in and out. Just Execute an Append query statement on a table set up for thi...

Removing User level security from an MS Access Database
I have secured a database in a network ebvironment. The workgroup information file is NOT the default file and the database is opened via a shortcut pointing at the relevant Workgroupninformation file. However, one particular form (the most heavily used form)now takes an age to open - 2 minutes or so, although once in it behaves acceptably. I have tried to remove the security, following the Help topic on this BUT... When I try to import or copy and paste the forms into the new database, I lose all the code attached to the event procedures in that form (or as the error message su...

Trouble when submitting sql-query to MS Access Database
Hi, I have some problems when trying to submit a simple insertion statement to a MS Access Database. The reading is fine, but when I want to execute an insertion statement using the following code: public void SQLCommand(String sql) throws SQLException { connection = DriverManager.getConnection(database); Statement stmt = connection.createStatement(); stmt.executeUpdate(sql); connection.close(); } gives the following error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Un...

how do you update a java.sql.date type in an ms access database
when i update a row in an access database the date field does not update to the correct date it brings up some funny date which i dont know where it comes from. Below is the method i use to update. maybe the date types are not compatable? thanks in advance, public static void bookIN(int custID, int rNumber,java.sql.Date bkInDate ){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String filename = "hotelDB.mdb"; String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; database+=...

Blocking users from accessing Access database
Hi, All I try to block some one import my Access database tables using Access database. I used password protection, but if some one crack through my password, are there any other methods to block some one importing my tables using Access database? Thanks kai "kai" wrote > I used password protection, but if some > one crack through my password, are > there any other methods to block some > one importing my tables using Access > database? No, not that I know of, if they break security (and it isn't all that difficult). Security on a desktop dat...

Wanted: Programmer to modify MS Access database for web use
I have a Microsoft Access database I'd like to have completed. I need forms added to add, delete and modify customers, sales and products. If you can also do the additional modifications to interface to my website then that would be a bonus. ...

Is this possible? please help (MS access query of sql database)
I work for an organisation that uses a bespoke document imaging system, the database of which is an MS sql server. We have MS Access and already use it for some querying of the database. The database comprises a large number of distinct cases, which are differentiated by case reference numbers, in one field (table?) of the database. Each of these cases may have many documents associated with it, denoted by the reference number, and these documents may be "new", "pending" or "complete" shown in another data field. We need to know how many cases have work outstandin...

Web resources about - query ms access database from the user - comp.lang.java.programmer

Database - Wikipedia, the free encyclopedia
A database is an organized collection of data . The data are typically organized to model aspects of reality in a way that supports processes ...

Database - Wikipedia, the free encyclopedia
... requiring information. For example, modelling the availability of rooms in hotels in a way that supports finding a hotel with vacancies. Database ...

Social Side of Code, Database CI and REST API Testing in Methods & Tools Winter 2015 issue
... for software developers, testers and project managers – has published its Winter 2015 issue that discusses the social side of code, database ...

Enterprises want to run databases in containers
More than three out of four IT decision makers are interested in running stateful applications like databases within containers, according to ...

Huge database of 191 million U.S. voters found on the open Internet
... such bulk information online, a recent discovery seems to indicate. According to VentureBeat , a security researcher found the exposed database ...

Top US School District Switches to Rimini Street for Oracle Database and Oracle E-Business Suite Support ...
Douglas County School District using savings from Rimini Street independent support to develop district-wide data visualization capabilities ...

IDG Contributor Network: Shrinking an already ginormous SharePoint database transaction log
Last month, I wrote about how to avoid having ginormous transaction logs within SharePoint databases. But what if it’s too late, and your files ...

18 million targeted voter records exposed by database error
A second database containing voter records has been exposed due to a configuration error. While the first held records that some dismissed inconsequential ...

Former Ohio cop uses cop database to stalk victim
... stalked her like an average person would, but stalked her in the way that only a police officer could: he stalked her utilizing a police database ...

U.S. Attorney Suggests Password Cardinals Used To Access Astros Scouting Database Was “Eckstein123"
... scouting director Christopher Correa plead guilty to five counts of unauthorized access to a computer for hacking the Houston Astros database. ...

Resources last updated: 2/5/2016 6:58:07 AM