jdbc 연결 데이터베이스 예제

9815 단어 데이터 뱅 크
java. sql. DriverManager 는 드라이버 를 불 러 와 데이터베이스 연결 을 가 져 옵 니 다.
java. sql. connection 에서 지정 한 데이터베이스 연결 완료
java. sql. Statement 은 주어진 연결 에서 SQL 로 성명 을 수행 하 는 용기 로 두 가지 중요 한 하위 형식 을 포함 하고 있 습 니 다.
자바. sql. Prepared Satement 은 사전 컴 파일 된 sql 성명 을 수행 하 는 데 사 용 됩 니 다.
자바. sql. CallableStatement 은 데이터베이스 에 저 장 된 프로 세 스 호출 을 수행 하 는 데 사 용 됩 니 다.
java. sql. ResultSet 에서 주어진 성명 에 대한 결 과 를 얻 는 경로
예제 코드 - my sql 연결:

package cn.itcast.cd.jsp.domain;

import java.util.Date;

public class Student {
private int id;
private String name;
private String sex;
private int age;
private Date birthday;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", age=" + age + ", birthday=" + birthday + "]";
}
}

package cn.itcast.cd.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* JDBC
* @author dendy
* @version 1.0
*/
public class DbUtils {
private static String url = "jdbc:mysql://localhost/jdbc";
private static String user = "root";
private static String password = "admin";

private DbUtils(){ //

}

// ,
static {
try {
// Class.forName com.mysql.jdbc.Driver , Driver , .
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
*
* @return Jave.sql.Connection , null.
*/
public static Connection getConnection(){
try {
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

/**
* Connection、Statement、ResultSet .
* @param connection
* @param statement
* @param resultSet
*/
public static void close(Connection connection, Statement statement, ResultSet resultSet){
close(connection);
close(statement);
close(resultSet);
}

/**
* Connection、Statement .
* @param connection
* @param statement
*/
public static void close(Connection connection, Statement statement){
close(connection);
close(statement);
}

/**
* Connection
* @param connection
*/
public static void close(Connection connection){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Statement
* @param statement
*/
public static void close(Statement statement){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* ResultSet
* @param resultSet
*/
public static void close(ResultSet resultSet){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

package cn.itcast.dao.Impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.cd.jsp.dao.IStudentDao;
import cn.itcast.cd.jsp.domain.Student;
import cn.itcast.cd.utils.DbUtils;

public class StudentDao implements IStudentDao {

@Override
public void add(Student stu) {
//
Connection connection = DbUtils.getConnection();

PreparedStatement preparedStatement = null;
String sql = "INSERT INTO student VALUES (null,?,?,?,?)";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, stu.getName());
preparedStatement.setString(2, stu.getSex());
preparedStatement.setInt(3, stu.getAge());

// java.sql.Date , java.util.Date ,
preparedStatement.setDate(4, new Date(stu.getBirthday().getTime()));

preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//
DbUtils.close(connection, preparedStatement);
}
}

@Override
public void delete(int id) {
//
Connection connection = DbUtils.getConnection();

PreparedStatement preparedStatement = null;
String sql = "DELETE FROM student WHERE id = " + id;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//
DbUtils.close(connection, preparedStatement);
}
}

@Override
public void update(int id, Student stu) {
//
Connection connection = DbUtils.getConnection();

PreparedStatement preparedStatement = null;
String sql = "UPDATE student SET name=?,sex=?,age=?,birthday=? WHERE ID="
+ id;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, stu.getName());
preparedStatement.setString(2, stu.getSex());
preparedStatement.setInt(3, stu.getAge());
preparedStatement.setDate(4, new Date(stu.getBirthday().getTime()));

preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//
DbUtils.close(connection, preparedStatement);
}
}

@Override
public Student get(int id) {
//
Connection connection = DbUtils.getConnection();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
String sql = "SELECT * FROM student WHERE ID=" + id;
try {
preparedStatement = connection.prepareStatement(sql);

resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Student stu = new Student();
stu.setName(resultSet.getString("name"));
stu.setSex(resultSet.getString("sex"));
stu.setAge(resultSet.getInt("age"));
stu.setBirthday(resultSet.getDate("birthday"));
stu.setId(resultSet.getInt("id"));

return stu;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//
DbUtils.close(connection, preparedStatement, resultSet);
}
return null;
}

@Override
public List getAll() {
List stuList = new ArrayList();
//
Connection connection = DbUtils.getConnection();
ResultSet resultSet = null;

PreparedStatement preparedStatement = null;
String sql = "SELECT * FROM student";
try {
preparedStatement = connection.prepareStatement(sql);

resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Student stu = new Student();
stu.setName(resultSet.getString("name"));
stu.setSex(resultSet.getString("sex"));
stu.setAge(resultSet.getInt("age"));
stu.setBirthday(resultSet.getDate("birthday"));
stu.setId(resultSet.getInt("id"));

stuList.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//
DbUtils.close(connection, preparedStatement, resultSet);
}
return stuList;
}

@Override
public List get(String name) {
List stuList = new ArrayList();
//
Connection connection = DbUtils.getConnection();
ResultSet resultSet = null;

PreparedStatement preparedStatement = null;
String sql = "SELECT * FROM student WHERE name='"+name+"'";
try {
preparedStatement = connection.prepareStatement(sql);

resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Student stu = new Student();
stu.setName(resultSet.getString("name"));
stu.setSex(resultSet.getString("sex"));
stu.setAge(resultSet.getInt("age"));
stu.setBirthday(resultSet.getDate("birthday"));
stu.setId(resultSet.getInt("id"));

stuList.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//
DbUtils.close(connection, preparedStatement, resultSet);
}
return stuList;
}

}

좋은 웹페이지 즐겨찾기