473,480 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Java Statement passing variables to MySQL Statement

7 New Member
Please to help me to following problem

I want to do this
1. create Table Name MEMBER on the Database Name "mytestdb",
2. Add the Values to the Table through the Key board Inputs during running the My Java Application.; Therefore I used this following codes, It Consists the seperate parts for the Raede and Member Class for the purposely I craetes.

3. I have problem to Parsing the values to SQL statement, that Consists on the Run Time values, It shows me in following codes Syntax error on this block of Codes

Expand|Select|Wrap|Line Numbers
  1. query = "INSERT INTO MEMBER(Member_ID,University_ID,Name," +
  2.                           "Registration_Date,Catergory,Renewed_Date)" +
  3.                           " VALUES(mbr.getMemberId(),mbr.getUniversityId()," +
  4.                           "mbr.getName(),mbr.getRegisterdDate(),mbr.getRegisteredCategory()," +
  5.                           "mbr.getRenewalDate() )";
  6.           stm.executeUpdate(query);

So, My Question Is How Can I Pass the Values through the Variable on Run time to MYSQL

I need urgently to solve this problem.
USED OS Windows, ECLIPSE IDE Used
mbr.getRegisterdDate(), In here mbr is Member type Object That I created, it will give the String return Value.

Thank you.
Joshep


Expand|Select|Wrap|Line Numbers
  1. package pretest;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Calendar;
  10.  
  11. //This is the library system Version 1.1.1.(20.02.2008)on Thursday
  12. public class LibJDBCv1 {
  13.  
  14.     /**
  15.      * @param args
  16.      */
  17.     public static void main(String[] args) {
  18.         System.out.println("MySQL Connect Example Modfied version ."); 
  19.         Connection conn = null;
  20.         String url = "jdbc:mysql://localhost:3306/";
  21.         String dbName = "mytestdb";
  22.         String driver = "com.mysql.jdbc.Driver";
  23.         String userName = "dbadmin"; 
  24.         String password = "dbadmin";
  25.         try {
  26.           Class.forName(driver).newInstance();
  27.           conn = DriverManager.getConnection(url+dbName,userName,password);
  28.  
  29.           System.out.println("Connected to the "+ dbName+" database");
  30.           Statement stm = conn.createStatement();
  31.  
  32.           // CREATES Table Name Member
  33.           String query;
  34.           try{
  35.               stm.executeUpdate("DROP TABLE MEMBER");
  36.               System.out.println("Table Deletion process is completly successfully!");
  37.             }
  38.             catch(SQLException s){
  39.               System.out.println("MEMBER Table is not exists!");
  40.             }
  41.  
  42.  
  43.  
  44.           query = "CREATE TABLE MEMBER(Member_ID CHAR (10) NOT NULL, "
  45.               +"University_ID CHAR (10),"+"Name VARCHAR (100),"
  46.               +"Registration_Date DATE,"+ "Catergory CHAR(3) NOT NULL," 
  47.               +"Renewed_Date DATE,"+"PRIMARY KEY (Member_ID))";
  48.  
  49.           stm.executeUpdate(query);
  50.           /* Member database Table Created ......................................................*/
  51.  
  52.  
  53.           System.out.println(" South Eastern University Library System \n");
  54.  
  55.             Member mbr= new Member();
  56.  
  57.             String nm = Reade.readentry("Enter Name                     :  ");
  58.                         mbr.setName(nm); // mbr is an instace of the member Class
  59.             nm = Reade.readentry("Enter Member ID                 :  ");
  60.                         mbr.setMemberId(nm); // here we have to chang this, that as automatic adding the  Member ID
  61.             nm = Reade.readentry("Enter Membership Catergory    :  ");
  62.                         mbr.setRegisteredCategory(nm); 
  63.             nm = Reade.readentry("Enter University ID             :  ");
  64.                         mbr.setUniversityId(nm); 
  65.             Calendar cal = Calendar.getInstance(); // Start date receiver
  66.                         mbr.setRegisterdDate(cal.getTime()); // date set by the Date forMAT 
  67.                          mbr.setRenewalDate( mbr.getRegisterdDate()); // Innitially Bot are Same
  68.  
  69.  
  70.  
  71.           // Inser the Database Values
  72.         query = "INSERT INTO MEMBER(Member_ID,University_ID,Name," +
  73.                           "Registration_Date,Catergory,Renewed_Date)" +
  74.                           " VALUES(mbr.getMemberId(),mbr.getUniversityId()," +
  75.                           "mbr.getName(),mbr.getRegisterdDate(),mbr.getRegisteredCategory()," +
  76.                           "mbr.getRenewalDate() )";
  77.           stm.executeUpdate(query);
  78.  
  79.  
  80.  
  81.           System.out.println("\n Inserted Values For the Member class table on  "+dbName  );
  82.  
  83.  
  84.           // create user
  85.           query = "SELECT *  FROM  MEMBER";
  86.           ResultSet rs=stm.executeQuery(query);
  87.           Member mbrQ = new Member();
  88.           while (rs.next()){
  89.                   mbrQ.setMemberId(rs.getString("Member_ID"));
  90.                   mbrQ.setUniversityId(rs.getString("University_ID"));
  91.                   mbrQ.setName(rs.getString("Name"));
  92.                   mbrQ.setRegisterdDate(rs.getDate("Registration_Date"));
  93.                   mbrQ.setRegisteredCategory(rs.getString("Catergory"));
  94.                   mbrQ.setRenewalDate(rs.getDate("Renewed_Date"));
  95.                   System.out.println("\tMember_ID = "+ mbrQ.getMemberId()
  96.                                                   +"\tUniversity_ID=" + mbrQ.getUniversityId()
  97.                                                   + "\tName=" + mbrQ.getName()
  98.                                                   + "\tRegistration_Date="+  mbrQ.getRegisteredCategory()
  99.                                                   + "\tCatergory="+ mbrQ.getRegisteredCategory()
  100.                                                   +"\tRenewed_Date"+mbrQ.getRenewalDate());
  101.           }
  102.  
  103.           System.out.println("\n User 'dbadmin' accessed database succesfully");
  104.           System.out.println("\n God Thanks well come");
  105.           conn.close();
  106.           System.out.println("Disconnected from database");
  107.         } catch (Exception e) {
  108.           e.printStackTrace();
  109.         }
  110.     }
  111.  
  112. }
Feb 22 '08 #1
3 7678
ronverdonk
4,258 Recognized Expert Specialist
This looks more like a Java problem and you'll probably have a better chance on help there.

So I will copy the thread to the Java forum and leave it here, hoping that someone can help you out.

Ronald
Feb 22 '08 #2
BigDaddyLH
1,216 Recognized Expert Top Contributor
I think your problem is that you are writing Java code inside strings. As a simple example:

Expand|Select|Wrap|Line Numbers
  1. String s = "z = 3*x+y";
Getting this to execute as Java code is not a simple matter.

Your solution is actually almost a standard response when people get confused about embedding SQL in Java: don't use java.sql.Statement, use java.sql.PreparedStatement. I go so far as to suggest that you never use just Statement. Take the Sun tutorial on JDBC and carefully note the section on PreparedStatement:

http://java.sun.com/docs/books/tutorial/jdbc/index.html
Feb 22 '08 #3
Joshepmichel
7 New Member
Thank you BiGDAD, I thing you suggests some thing related to my question, I'll try to see the link.
I think your problem is that you are writing Java code inside strings. As a simple example:

Expand|Select|Wrap|Line Numbers
  1. String s = "z = 3*x+y";
Getting this to execute as Java code is not a simple matter.

Your solution is actually almost a standard response when people get confused about embedding SQL in Java: don't use java.sql.Statement, use java.sql.PreparedStatement. I go so far as to suggest that you never use just Statement. Take the Sun tutorial on JDBC and carefully note the section on PreparedStatement:

http://java.sun.com/docs/books/tutorial/jdbc/index.html
Feb 26 '08 #4

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

Similar topics

5
3361
by: apchar | last post by:
I am trying to use php as a kind of servlet to act as a middle man between a java applet and mysql. I know java has jdbc but it's flakey and painful. php access to mysql is much nicer. So I have:...
31
2545
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
4
1809
by: Rhino | last post by:
I've been playing with Java UDFs for the last couple of days and I've got some questions about scratchpads. I'm running DB2 LUW V8 (FP8) on WinXP. Somewhere in the manuals, I found some remarks...
458
20755
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
0
3230
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
21
3444
madhoriya22
by: madhoriya22 | last post by:
Hi, Here is the query which I am using to get the values from the database:- "SELECT ?, COUNT(*) AS COUNT " + "FROM DEFECT_DETAIL " + "WHERE TARGET_MILESTONE = ? " + "GROUP BY ?"; ...
3
6425
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
1
2465
by: swethak | last post by:
hi, when i run a java program for to store data and retrive using mysql datatabse i got the following errors.I think in that one of error is due to set the class path.I placed my...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
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,...
0
6861
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...
0
4468
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...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.