I had been improve my knowledge about java for 2 years.Today, I was started working with databases.I have 2 class that are DatabaseConnection and UserModel.Databaseconnection class have connecting operations and UserModel will add,remove,update etc. methods.I didn’t write remove and update code’s yet.I writed only add method.But It its adding to user.I searched many thinks about that but i cant fixed that.
This is my Connection Class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
/**
*
* @author KHn
*/
public abstract class DatabaseConnection {
private Connection connect;
private static final String dbUsername="root";
private static final String dbPassword="";
private static final String dbName="virtualblog";
private static final String dbHost="jdbc:mysql://localhost:3306/";
private String driver="com.mysql.jdbc.Driver";
private Statement st;
private ResultSet rs;
/** Creates a new instance of DatabaseConnection */
public DatabaseConnection(){
try {
Class.forName(this.driver).newInstance();
setConnect((Connection)DriverManager.getConnection(dbHost+dbName,dbUsername,dbPassword));
} catch (Exception e) {
System.out.println(e);
}
}
public Connection getConnect() {
return connect;
}
public void setConnect(Connection connect) {
this.connect = connect;
}
}
More Over, this is a UserModelClass:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.beans.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
/**
*
* @author KHn
*/
@ManagedBean
@RequestScoped
public class UserModel extends DatabaseConnection{
private PreparedStatement ps;
private Statement st;
private ResultSet ts;
Register member =new Register();
public Register getUser() {
return member;
}
public void setUser(Register member) {
this.member = member;
}
public boolean addUser(){
try{
ps=(PreparedStatement)getConnect().prepareStatement("INSERT INTO `members`(`id`, `username`, `email`, `password`, `name`, `avatar`, `blogname`) VALUES (null,?,?,'3694406','kaan','asdasd','blog adi')");
ps.setString(2, member.getUsername());
ps.setString(3, member.getEmail());
ps.executeUpdate();
}catch (Exception e) {
System.err.println("ekleme hatasi: "+e);
}
return true;
}
/** Creates a new instance of UserModel */
public UserModel() {
}
}
Where did i do wrong? Can you help me about that please.Thanks and Best Regards.
Your JDBC connection may be in transactional mode. I.e. (multiple) queries passed through the connection will only be really executed (in an isolation) when the connection is committed (or closed).
So, calling
connection.commit()orconnection.close()should do it.You’ve only a major design mistake in your code which makes it hard to implement this properly. You basically need to rewrite everything. To start, you should absolutely not have expensive DB resources like
Connection,StatementandResultSetas instance variables of a class. They should be declared, acquired and closed in the shortest possible scope, preferably in the very same method block as where you’re executing the SQL query. Also, all that JDBC job belongs in a separate DAO class, not in a managed bean. Your DAO code needs to be designed that way so that you ultimately end up like follows in youraddUser()method:To get a step further, consider learning EJB/JPA as well. EJBs allows for finer grained control over transactions and have a sane default behaviour (auto-commit by end of request, for example) and JPA results in less JDBC boilerplate code, all the JDBC code can basically be replaced by oneliners.