[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Mysql and java

Posted on 2006-05-12
13
Medium Priority
?
389 Views
Last Modified: 2010-03-31
im new to mysql so i dont know how to use preparedstatement in java

down i have wrote the code..i want to preparedstatement instead of using so many strings in sql
could u plz make the parepared statememnt for my java class

import java.sql.*;
/*
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class User
{
  //SQL connection
  Connection conn;
  Statement statement;
  String  sql = null;
 
  //Mysql driver
  private String driverName = "com.mysql.jdbc.Driver";
  private String serverName = "localhost";
  private String dataBase = "sikkerhedssystem";
  private String driverURL = "jdbc:mysql://" + serverName +  "/" + dataBase;
  private String userName = "root";
  private  String passw = "ilovenaima";
 
     
 
 
  public User() throws SQLException, ClassNotFoundException
  {
    Class.forName(driverName);
    conn = DriverManager.getConnection(driverURL, userName, passw);
    statement = conn.createStatement();
   
  }
 
  public void createUser(int uid, String username, String fname, String sname,
                         int accessLevel, Date birthDate, int phonenumber,
                         String ibutton, String password, String street,
                         int postcode, String city )
  {
    sql ="insert into user values";
   
   
0
Comment
Question by:Maggieshah
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16666411
You didn't post the entire query. Its like:

PreparedStatement ps = conn.prepareStatement ( "INSERT INTO TABLE (USER_NAME) VALUES (?)" ) ;
ps.setString ( 1, "MaggieShah" ) ;
ps.execute () ;
0
 

Author Comment

by:Maggieshah
ID: 16666418
i want it more specific
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16666425
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16666439
I posted a sample query with one table, one field. You can extend it for multiple values. Now if you give an actual specific query that you have, then only can I post what you want.
0
 

Author Comment

by:Maggieshah
ID: 16666460
make it like preparestatement

public void createUser(int uid, String username, String fname, String sname,
                         int accessLevel, Date birthDate, int phonenumber,
                         String ibutton, String password, String street,
                         int postcode, String city )
  {
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 16666493
What is the existing SQL query you have for this, I meant? I don't know what columns, tables, etc you have. I guess it would look like:

PreparedStatement ps = conn.prepareStatement ( "INSERT INTO USER (UID, USERNAME, FNAME, .... , CITY) VALUES (?, ?, ?, ....?) " ) ; // fill in the values for the columns and equal number of question-marks
ps.setInt ( 1, uid ) ;
ps.setString ( 2, username ) ;
.... // set other values
ps.setString ( 3, city ) ;
ps.execute () ;
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16666496
>> ps.setString ( 3, city ) ;

Sorry that would probably be ps.setString ( 12, city ) ;
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16667906
> could u plz make the parepared statememnt for my java class
here you'll learn what is PreparedStatement
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

0
 

Author Comment

by:Maggieshah
ID: 16676116
how can i make a delete method when i using prepare statement
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16677924
PreparedStatement updateSales = con.prepareStatement("DELETE FROM table_name WHERE column_name = ?");
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16679638
Then set the value using: updateSales.setString ( 1, "the value" ) ;
0
 
LVL 12

Expert Comment

by:Giant2
ID: 16682816
Little explain:
Preparing statement is usefull when you are doing the same query every time.
Like:
INSERT INTO xxx (a,b,c) VALUEs(1,2,3)
INSERT INTO xxx (a,b,c) VALUEs(11,22,33)
...
INSERT INTO xxx (a,b,c) VALUEs(111,222,333)
or
DELETE or UPDATE or other you need.

To use the preparestatement you can only use the above suggest by other experts noting you can replace the real value by a "?".
When you want to run the query with different values you must only set the changing values (they are replaced in order you put the "?") and after call the execute.
Example:
The above became:
INSERT INTO xxx (a,b,c) VALUES(?,?,?)

Is it now more clear?
Bye, Giant.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question