Link to home
Start Free TrialLog in
Avatar of Maggieshah
Maggieshah

asked on

Mysql and java

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";
   
   
Avatar of Mayank S
Mayank S
Flag of India image

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 () ;
Avatar of Maggieshah
Maggieshah

ASKER

i want it more specific
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.
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 )
  {
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> ps.setString ( 3, city ) ;

Sorry that would probably be ps.setString ( 12, city ) ;
> 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

how can i make a delete method when i using prepare statement
PreparedStatement updateSales = con.prepareStatement("DELETE FROM table_name WHERE column_name = ?");
Then set the value using: updateSales.setString ( 1, "the value" ) ;
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.