473,583 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ 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.getRegister dDate(), 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 7695
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.Statem ent, use java.sql.Prepar edStatement. 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 PreparedStateme nt:

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.Statem ent, use java.sql.Prepar edStatement. 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 PreparedStateme nt:

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
3374
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: 1. An html page that holds the applet. 2. a php page that accepts data submitted to it by the applet via the $_POST array and writes it to the mysql...
31
2570
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 insensitive string comparison, and a substring search. Then I found that there are no networking classes to speak of (except sockets). After I thought...
4
1824
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 that said I could either manage my scratchpad with the getScratchpad() and setScratchpad() methods *OR* set up my own class variables to keep the...
458
21005
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 simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they...
0
3252
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
3475
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 ?"; and here is function throught which I am getting data:-
3
6432
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
2483
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 mysql-connector-java-3.0.11-stable-bin jar in lib directory.What is the command to set a classpath in windows for java mysql connectivity.plz tell that .And also how...
0
7827
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8184
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. ...
1
7936
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...
0
6581
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...
1
5701
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...
0
5375
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...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2334
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 we have to send another system
0
1158
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.