1.DvdDao
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.spi.DirStateFactory.Result;
import com.minidvd.entity.DvdEntity;
public class DvdDao {
final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=minidvd";
final static String USER = "sa";
final static String PASSWORD = "123";
final static String SQLSERVER_CLASS_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
private Connection conn;
private Statement state;
private ResultSet resultSet;
public DvdDao() {
try {
Class.forName(SQLSERVER_CLASS_NAME);
} catch (ClassNotFoundException e) {
System.out.println("没加载sqlserver的jar包");
}
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("数据库连接失败");
}
try {
state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (SQLException e) {
e.printStackTrace();
}
}
public int add(String addsql) {
int rows = 0;
try {
rows = state.executeUpdate(addsql);
} catch (SQLException e) {
System.out.println("异常错误,添加失败");
}
return rows;
}
public int update(String updatesql) {
int rows = 0;
try {
rows = state.executeUpdate(updatesql);
} catch (SQLException e) {
System.out.println("异常错误,更新失败");
}
return rows;
}
public int delete(String deletesql) {
int rows = 0;
try {
rows = state.executeUpdate(deletesql);
} catch (SQLException e) {
System.out.println("异常错误,删除失败");
}
return rows;
}
public ResultSet find(String findsql) {
resultSet = null;
try {
resultSet = state.executeQuery(findsql);
} catch (SQLException e) {
System.out.println("异常出错,查询失败");
}
return resultSet;
}
public void close() {
try {
if (resultSet != null)
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (state != null)
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.DvdEntity
package com.minidvd.entity;
public class DvdEntity {
private int id;
private String name;
private int state;
private String date;
private int count;
/**
* 取得 name
* @return the name
*/
public DvdEntity(){
}
public DvdEntity(int id,