Skip to content

Instantly share code, notes, and snippets.

@symtkn
Created May 15, 2013 23:14
Show Gist options
  • Save symtkn/5588176 to your computer and use it in GitHub Desktop.
Save symtkn/5588176 to your computer and use it in GitHub Desktop.
DAO connection
package business;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ProjeDAO {
protected String connectionURL = "jdbc:mysql://localhost:3306/uyeler";
public int control(ProjeBean projeBean) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String quest = "select * from uye where Adi = ? AND password = ? ";
String userName = null;
String userPassword = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(connectionURL, "root", "symtkn");
pstmt = conn.prepareStatement(quest);
pstmt.setString(1, projeBean.getAdi());
pstmt.setString(2, projeBean.getPassword());
rs = pstmt.executeQuery();
while (rs.next()) {
userName = rs.getString("Adi");
userPassword = rs.getString("password");
}
if(userName == null && userPassword == null){
return 0;
}
} catch (Exception e) {
throw e;
}finally {
try {
pstmt.close();
} catch (Exception e2) {
}
try {
conn.close();
} catch (Exception e2) {
}
}
return 1;
}
public ProjeBean insertProjeBean(ProjeBean projeBean) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
String sqlStr = "insert into uye(Adi, Soyadi, password, eposta)";
sqlStr += "values(?, ?, ?, ?)";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(connectionURL, "root", "symtkn");
if(control(projeBean) == 0){
pstmt = conn.prepareStatement(sqlStr);
pstmt.setString(1, projeBean.getAdi());
pstmt.setString(2, projeBean.getSoyadi());
pstmt.setString(3, projeBean.getPassword());
pstmt.setString(4, projeBean.getEposta());
pstmt.executeUpdate();
}
} catch (Exception e) {
throw e;
}finally {
try {
pstmt.close();
} catch (Exception e2) {
}
try {
conn.close();
} catch (Exception e2) {
}
}
return projeBean;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment