Circle.java
package org.yash.watertechsol.model;
public class Circle {
private int id;
private String name;
public Circle(int circleId,
String name){
setId(circleId);
setName(name);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String
getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
}
JdbcDaoImpl.java
package org.yash.watertechsol.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.yash.watertechsol.model.Circle;
public class JdbcDaoImpl {
public Circle getCircle(int circleId){
Connection conn =null;
try{
String
driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","");
PreparedStatement
ps = conn.prepareStatement("select * from circle where id=?");
ps.setInt(1,
circleId);
Circle
circle =null;
ResultSet
rs = ps.executeQuery();
if(rs.next()){
circle
= new
Circle(circleId, rs.getString("name"));
}
rs.close();
ps.close();
return
circle;
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
JdbcDemo.java
package org.yash.watertechsol;
import org.yash.watertechsol.dao.JdbcDaoImpl;
import org.yash.watertechsol.model.Circle;
public class JdbcDemo {
public static void main(String[]
args){
Circle circle = new
JdbcDaoImpl().getCircle(1);
System.out.println(circle.getName());
}
}
No comments:
Post a Comment