String user = request.getParameter("uname");
String pwd = request.getParameter("pass");
String pwd1 = request.getParameter("pass");
String pwd2 = request.getParameter("pass");
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/logindb";
Connection con = DriverManager.getConnection(url, "root", "root");
String sql = "select * from register";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, request.getParameter("uname"));
ResultSet rs = ps.executeQuery();
if(rs.next())
{
sql = "update register set pass=? where uname=? ";
ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, request.getParameter("uname"));
ps.executeUpdate();
out.println("password changed");
}
}
catch(Exception e)
{
out.println(e);
}
This Is my code and it will show me an error like this
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).**
Here:
String sql = "select * from register";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, request.getParameter("uname"));
You are trying to set parameters that aren't there. Looks like a copy-and-paste error. Those last 2 lines shouldn't be there.
Related
hi im new to coding stuffs and i really want to know how to set jLabel with a data from sql server.
i got some codes but its for table and not Label, also it required us to insert the value first so it can be set. someone please help me.
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=GUGUNet";
Connection con = DriverManager.getConnection(url, "sa","gugu");
String query = "insert into Home.HomeTable (Nama, Durasi, PC)VALUES(?,?,?)";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, nama.getText());
ps.setString(2, (String) homecombo.getSelectedItem());
ps.setString(3, pc.getText());
if (nama.getText().trim().matches("[a-z A-Z]+") && pc.getText().trim().matches("[0-9]+") && homecombo.getSelectedItem()!="0")
{
ps.executeUpdate();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select ID, Nama From Home.HomeTable");
etable.setModel(DbUtils.resultSetToTableModel(rs));
reset();
//insert();
JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan");
}
else
{
JOptionPane.showMessageDialog(null, "Silahkan isi data dengan benar");
}
I'm trying to make a register page into my servlet project.
If the username is already taken, I want to show an error message.
Heres my code :
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/covid","root","fbfbfb333*");
String query = "SELECT * FROM users WHERE username = ?";
pstmt = (PreparedStatement) con.prepareStatement(query);
pstmt.setString(1, uname);
rs = pstmt.executeQuery();
if(rs.next()) {
PreparedStatement pst = (PreparedStatement) con.prepareStatement(" insert into users(username,password) values(?,?)");
pst.setString(1, uname);
pst.setString(2, pass);
pst.executeUpdate();
response.sendRedirect("login.jsp");
} else {
System.out.println("Username " + uname + " already exists.");
}
} catch (Exception e) {
e.printStackTrace();
}
But when I add a new users , it says " username already taken " whether it exist or not.
What should I do?
Switch the condition, if there is a result set data means that the username is taken, else insert new username
if(rs.next()) {
System.out.println("Username " + uname + " already exists.");
} else {
PreparedStatement pst = (PreparedStatement) con.prepareStatement(" insert into users(username,password) values(?,?)");
pst.setString(1, uname);
pst.setString(2, pass);
pst.executeUpdate();
response.sendRedirect("login.jsp");
}
I want to insert logical operations (=,<,>=,<,>=) in my sql
try {
String sql = "SELECT id,name,date_N FROM LECTEUR WHERE date_N ?,?";
Connection con = DBinfo.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
ps.setString(1, operationCB.getValue());
ps.setString(2, date1.toString());
ResultSet resultSet = ps.executeQuery();
output:
Incorrect syntax near '#P0'.
You can only insert values as parameters of the PreparedStatement.
If you want the operation to be dynamic, use a StringBuilder to build the query String:
StringBuilder sql = new StringBuilder();
sql.append("SELECT id,name,date_N FROM LECTEUR ");
sql.append("WHERE date_N ");
sql.append(operationCB.getValue());
sql.append(" ?");
Connection con = DBinfo.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql.toString());
ps.setString(1, date1.toString());
ResultSet resultSet = ps.executeQuery();
You cannot bind a logical operator to a prepared statement using a ? placeholder. One workaround would be to maintain separate queries for the various logical operations:
Map<String, String> queryMap = new HashMap<>();
queryMap.put("=", "SELECT id, name, date_N FROM LECTEUR WHERE date_N = ?");
queryMap.put(">", "SELECT id, name, date_N FROM LECTEUR WHERE date_N > ?");
// etc.
try {
String sql = queryMap.get(operationCB.getValue()); // fetch the appropriate statement
Connection con = DBinfo.getConnection();
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
ps.setDate(1, date1);
ResultSet resultSet = ps.executeQuery();
} catch (SQLException e) {
// handle exception
}
I am trying it from last 2 hour i dont know what is going wrong. also let me know how could i test if the connection is successfull or not.. it is for now.. but still i wanted to know..
Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?)'
My code:
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/medicinedb";
static final String USER = "root";
static final String PASS = "";
static final String DRIVER = "com.mysql.jdbc.Driver";
public boolean saveMedicine(List<MedicineName> medicineName) throws ClassNotFoundException {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DRIVER);
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
for (MedicineName element : medicineName) {
String sql;
sql = " insert into medicinename (name, pgurl)" + " values
( ?, ?)";
System.out.println(conn);
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, element.getName());
preparedStmt.setString(2, element.getPgurl());
stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Why you are using Statement and Prepared statement. You are performing operations with Statement. Thus it does not have value. Remove the Statement
stmt = conn.createStatement();
And do the following
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, element.getName());
preparedStmt.setString(2, element.getPgurl());
//stmt.executeUpdate(sql);
preparedStmt .executeUpdate(sql);
use
String sql = "insert into medicinename (name, pgurl) values (?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString (1, element.getName());
preparedStmt.setString (2, element.getPgurl());
preparedStmt.executeUpdate();
you should delete executeUpdate(sql); to executeUpdate();
You mix between your stm and preparedStmt, you have to execute preparedStmt and not stm.
Another thing when you want to executing multiple statements as one unit, its good to work with Statement Batching instead for example :
PreparedStatement preparedStmt;
String sql;
conn.setAutoCommit(false);
for (MedicineName element : medicineName) {
sql = "insert into medicinename (name, pgurl) values (?, ?)";
System.out.println(conn);
preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, element.getName());
preparedStmt.setString(2, element.getPgurl());
preparedStmt.addBatch(sql);
}
preparedStmt.executeBatch();
conn.commit();
I want to retrieve a users name from the username and password that they have entered into textfields. i want to pass these values as parameter to method which will then identify the users name for printout. please help. something like this...
public void getUser(String username, String password)throws SQLException{
String qry = "SELECT UserName From USER....";
Connection con = null;
PreparedStatement stmt = null;
try{
con = DriverManager.getConnection("URL");
stmt = con.prepareStatement(qry);
stmt.executeUpdate();
If not is an update or an insert, don't use executeUpdate()
You must use executeQuery().
Try this:
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String qry = "SELECT UserName From USER where username=? and password=?";
try{
con = DriverManager.getConnection("URL");
stmt = con.prepareStatement(qry);
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("UserName"));
}
...
Regards
Assuming that you want to get the user's full name from db and your table structure is something like this:
fullname | username | password
you could do the following:
Connection c = null;
PreparedStatement s = null;
try {
c = DriverManager.getConnection("URL");
s = c.prepareStatement("SELECT fullname FROM user WHERE username=? and password=?");
s.setString(1, username);
s.setString(2, password);
ResultSet rs = s.executeQuery();
if(rs.next())
return rs.getString("fullname");
return null; // no user found!
} catch(SQLException e) {
System.err.println(e.getMessage());
} finally {
// close s and c
}
Note: This assumes that the password that is passed to the method is in the same "form" as it is stored in db (i.e. either plain text or hashed (+salted))
try this its very simple example
private boolean validate_login(String id,String password) {
try{
Class.forName("org.sqlite.JDBC"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:sqlite:studentdb.sqlite");
PreparedStatement pst = conn.prepareStatement("Select * from student_table where id=? and password=?");
pst.setString(1, id);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
return true;
else
return false;
try{
Class.forName("org.sqlite.JDBC"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:sqlite:studentdb.sqlite");
PreparedStatement pst = conn.prepareStatement("Select * from student_table where id=? and password=?");
pst.setString(1, id);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
return true;
else
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}