473,657 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't insert java variable into ms access

1 New Member
Hi All,
I can't insert java variable into ms access database.
I'm using odbc connection to ms access.
Below are my coding.
Expand|Select|Wrap|Line Numbers
  1.  try 
  2.         {
  3.         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");        
  4.         // set the connection for database path
  5.         String Database_Path = "E:/Segi/Project/Coding/Car_Park_Storage.mdb";
  6.         String Storage = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
  7.         Storage+= Database_Path.trim() + ";DriverID=22;READONLY=true}";          
  8.         Connection Connect = DriverManager.getConnection( Storage ,"",""); 
  9.         Statement Insert = Connect.createStatement();
  10.         //int date_in = Integer.parseInt(Show_Date_In);
  11.         //int times_in = Integer.parseInt(Show_Times_In);
  12.         Insert.execute("insert into Daily_Details values(Car_Number,Parking_Lot,'13','14','345','345','456','3456');"); 
  13.  
  14.         Insert.close(); 
  15.         Connect.close(); 
  16.         }
  17.  
When I change the variable to values like ('11','12','13' ,'14','345','34 5','456','3456' );");
It able to insert into access.
Kindly please help look into the problem.

Thanks and Regards,
Crispin Yeap
Feb 23 '08 #1
2 6102
Dököll
2,364 Recognized Expert Top Contributor
Hey there good buddy!

Looks like a great written code but I cannot see what the problem is. The only solution I have for you is giving something that works for me:

JavaBean:

Expand|Select|Wrap|Line Numbers
  1. package foo;
  2.  
  3. /**
  4.  * @author Dököll
  5.  * 
  6.  */
  7.  
  8. import java.io.*;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.Statement;
  12. import java.sql.ResultSet;
  13.  
  14. public class AddBean {
  15.  
  16.     private String EmployeeID;
  17.     private String UserID;
  18.  
  19.     private String LastName;
  20.     private String FirstName;
  21.  
  22.     private String Title;
  23.     private String TitleCourtesy;
  24.  
  25.     private String BirthDate;
  26.     private String HireDate;
  27.  
  28.     private String Address;
  29.  
  30.     private Connection connection = null;
  31.     private ResultSet rs = null;
  32.     private Statement st = null;
  33.     String connectionURL = "jdbc:odbc:Northwind";
  34.  
  35.     public AddBean() {
  36.         try {
  37.             // Load the database driver
  38.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  39.             // Get a Connection to the database
  40.             connection = DriverManager.getConnection(connectionURL, "root",
  41.                     "root");
  42.         } catch (Exception e) {
  43.             System.out.println("Exception is ;" + e);
  44.         }
  45.  
  46.     }
  47.  
  48.     public void setUserID(String UserID) {
  49.         this.UserID = UserID;
  50.     }
  51.  
  52.  
  53.  
  54.     public String getUserID() {
  55.         return (this.UserID);
  56.     }
  57.  
  58.  
  59.     public void SendInTheClowns() {
  60.  
  61.         try {
  62.             String sql = "insert into Employees(EmployeeID, UserID,LastName, FirstName, Title, TitleCourtesy,BirthDate, HireDate, Address )values('"
  63.                     +EmployeeID + "','" + UserID + "','" + LastName + "','" + FirstName + "','" + Title + "','" + TitleCourtesy + "','" + BirthDate + "','" + HireDate + "','" + Address + "')";
  64.             Statement s = connection.createStatement();
  65.             s.executeUpdate(sql);
  66.             s.close();
  67.         } catch (Exception e) {
  68.             System.out.println("Exception is ;" + e);
  69.         }
  70.     }
  71.  
  72.     public String getEmployeeID() {
  73.         return EmployeeID;
  74.     }
  75.  
  76.     public void setEmployeeID(String employeeID) {
  77.         EmployeeID = employeeID;
  78.     }
  79.  
  80.     public String getLastName() {
  81.         return LastName;
  82.     }
  83.  
  84.     public void setLastName(String lastName) {
  85.         LastName = lastName;
  86.     }
  87.  
  88.     public String getFirstName() {
  89.         return FirstName;
  90.     }
  91.  
  92.     public void setFirstName(String firstName) {
  93.         FirstName = firstName;
  94.     }
  95.  
  96.     public String getTitle() {
  97.         return Title;
  98.     }
  99.  
  100.     public void setTitle(String title) {
  101.         Title = title;
  102.     }
  103.  
  104.     public String getTitleCourtesy() {
  105.         return TitleCourtesy;
  106.     }
  107.  
  108.     public void setTitleCourtesy(String titleCourtesy) {
  109.         TitleCourtesy = titleCourtesy;
  110.     }
  111.  
  112.     public String getBirthDate() {
  113.         return BirthDate;
  114.     }
  115.  
  116.     public void setBirthDate(String birthDate) {
  117.         BirthDate = birthDate;
  118.     }
  119.  
  120.     public String getHireDate() {
  121.         return HireDate;
  122.     }
  123.  
  124.     public void setHireDate(String hireDate) {
  125.         HireDate = hireDate;
  126.     }
  127.  
  128.     public String getAddress() {
  129.         return Address;
  130.     }
  131.  
  132.     public void setAddress(String address) {
  133.         Address = address;
  134.     }
  135. }
  136.  
Your JSP

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ page language="java"
  3.     pageEncoding="ISO-8859-1"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8.  
  9.  
  10. <%@ page import="java.sql.*"%>
  11. <title>Submit form...</title>
  12. </head>
  13.  
  14. <body bgcolor="#ffccff">
  15. <h1>Submit form</h1>
  16. <form name="form1" method="POST" action="">
  17.  
  18. EmployeeID:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><input
  19.     type="text" name="employeeID"> <br>
  20. User ID:<br><input type="text" name="userID"> <br>
  21.  
  22. Last Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><input
  23.     type="text" name="lastName"> <br>
  24. First Name:<br><input type="text" name="firstName"> <br>
  25.  
  26. Title:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><input
  27.     type="text" name="title"> <br>
  28. Title Courtesy:<br><input type="text" name="titleCourtesy"> <br>
  29.  
  30. Birth Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><input
  31.     type="text" name="birthDate"> <br>
  32. Hire Date:<br><input type="text" name="hireDate"> <br>
  33.  
  34. Address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><input
  35.     type="text" name="address"> <br>
  36. <br>
  37. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" value="Submit">
  38. <jsp:useBean id="sample" class="foo.AddBean" scope="page">
  39.     <jsp:setProperty name="sample" property="*" />
  40. </jsp:useBean></form>
  41.  
  42.  
  43. <%
  44.     sample.SendInTheClowns();
  45. %>
  46.  
  47.  
  48. </body>
  49. </html>
  50.  
I'll tell ya, this works like waterfalls, sure you'll need to modify to your access database fields to relate to TEXT rather than Number, but, you should be good to go...

You'll need to creat an ODBC connection called Northwind or anything you want:

(1) Control Panel
(2) Administrative Tools
so on and so forth:-)

Gotta go now, hope this works:-)
Dec 11 '08 #2
r035198x
13,262 MVP
1.) You must wrap the values around single quotes when inserting data into character columns in the database. That's why ('11','12','13' ,'14','345','34 5','456','3456' ); works. Using a PreparedStateme nt takes care of the problem.

2.) What are Car_Number and Parking_Lot? Are they supposed to be variables holding the actual values?
Dec 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5152
by: Nikhil Barthwal | last post by:
Hi, I have a Java program that needs to access the value of variable PATH (Set in Unix enviroment by setenv or DOS enviroment by PATH statement). How do I access it in Java? Thanks in advance, Nikhil
3
7723
by: Nelson Broat | last post by:
In jsp land you can have the following: <% String name = "Nelson"; %> Hi, my name is <%= name %>. Such that, in your browser you see:
1
4963
by: 2BaCook | last post by:
Hi, I have an Access database that I am creating a java front end to. I am trying to insert a record into a table and get a problem whenever I try to insert into my "Number" column. Here is my table schema: Column 1 Name: Number Type: VARCHAR Column 2 Name: Street Type: VARCHAR Column 3 Name: Phone Type: VARCHAR Column 4 Name: Comments Type: LONGCHAR Column 5 Name: Pets Type: VARCHAR
22
23347
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
4
12427
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
2145
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include file="../Connections/conn.jsp" %> <% // *** Edit Operations: declare variables // set the form action variable String MM_editAction = request.getRequestURI(); if (request.getQueryString() != null && request.getQueryString().length() > 0) {
5
5742
by: techbuddha | last post by:
Hi new to the forum and visual basic. I am attempting to fix a migration of excel to access. The excel sheets where simple copied as is into access. for example one table lists the academic history from elementary to phd and on to post doctoral work alll on the same row. I want to break that up to have each level of education as a seperate record and then relate that back to the person. i can pull the data into a recordset I can...
7
3654
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. Can C++ directly acess the Registers. Will the Computation 5-10 times faster than Java?
2
3322
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a property IssueData. this property exist in SimpleBean which create a connection from DB and insert the data. At run time servlet and server also show that loggging are saved in DB. But when I open the table in Access. Its empty. Ms-Access have...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.