JDBC 连接SQLSERVER2008数据库

时间:2022-03-01 07:41:06

JDBC 连接SQLSERVER2008数据库


package DAO;

import java.util.List;

import Entity.Student;

public interface IStuManagerDao {

public int addStudent(Student stu);

public int delStudent(int stuId);

public int updateStudent(Student stu);

public Student selStudentById(int stuId);

public List<Student> selStudent();

}


package DAO.Impl;

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

import util.DBManager;

import DAO.IStuManagerDao;
import Entity.Student;

public class StuManagerDaoImpl implements IStuManagerDao {

Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;

List<Student> stuList = null;

DBManager dbm = new DBManager();

@Override
public int addStudent(Student stu) {
int count =0;
try {
conn = dbm.getConn();
String sql = "insert into Student values(?,?)";
pstm = conn.prepareStatement(sql);
pstm.setString(1, stu.getStuName());
pstm.setInt(2,stu.getStuAge());
count = pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
dbm.closeAll(conn, pstm, rs);
}
return count;
}

@Override
public int delStudent(int stuId) {
// TODO Auto-generated method stub
return 0;
}

@Override
public int updateStudent(Student stu) {
// TODO Auto-generated method stub
return 0;
}

@Override
public Student selStudentById(int stuId) {
// TODO Auto-generated method stub
return null;
}

@Override
public List<Student> selStudent() {
stuList = new ArrayList<Student>();
try {
conn = dbm.getConn();
String sql = "select * from student";
pstm = conn.prepareStatement(sql);

rs = pstm.executeQuery();
Student stu = null;
while(rs.next())
{
stu = new Student();
stu.setStuId(rs.getInt("stuId"));
stu.setStuName(rs.getString(2));
stu.setStuAge(rs.getInt(3));

stuList.add(stu);
}

} catch (Exception e) {
e.printStackTrace();
}
finally
{
dbm.closeAll(conn, pstm, rs);
}
return stuList;
}

}


package Service;

import java.util.List;

import Entity.Student;

public interface IStuManagerService {

public Boolean addStudent(Student stu);

public Boolean delStudent(int stuId);

public Boolean updateStudent(Student stu);

public Student selStudentById(int stuId);

public List<Student> selStudent();

}


package Service.Impl;

import java.util.List;

import DAO.Impl.StuManagerDaoImpl;
import Entity.Student;
import Service.IStuManagerService;

public class StuManagerServiceImpl implements IStuManagerService {

StuManagerDaoImpl smi = new StuManagerDaoImpl();

@Override
public Boolean addStudent(Student stu) {
int count = smi.addStudent(stu);
if(count>0)
{
return true;
}
return false;
}

@Override
public Boolean delStudent(int stuId) {
int count = smi.delStudent(stuId);
if(count>0)
{
return true;
}
return false;
}

@Override
public Boolean updateStudent(Student stu) {
int count = smi.updateStudent(stu);
if(count>0)
{
return true;
}
return false;
}

@Override
public Student selStudentById(int stuId) {
Student stu = smi.selStudentById(stuId);
if(stu != null)
{
return stu;
}
return null;
}

@Override
public List<Student> selStudent() {
List<Student> stuList = smi.selStudent();
if(stuList.size()>0)
{
return stuList;
}
return null;
}

}


package util;

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

public class DBManager {
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Demo";
private static final String user = "sa";
private static final String pwd = "123123";

Connection conn = null;

public Connection getConn() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("驱动连接成功");
} catch (Exception e) {
e.printStackTrace();
}

return conn;
}

public void closeAll(Connection conn, Statement sm, ResultSet rs) {

try {
if (rs != null) {
rs.close();
rs = null;
}
if (sm != null) {
sm.close();
sm = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();

}
}

}


package Test;

import java.util.List;
import java.util.Scanner;

import Entity.Student;
import Service.Impl.StuManagerServiceImpl;

public class Test {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
StuManagerServiceImpl smsi = new StuManagerServiceImpl();
Student stu = new Student();
sc = new Scanner(System.in);
System.out.println("请输入学生姓名");
stu.setStuName(sc.next());
System.out.println("请输入学生姓名");
stu.setStuAge(sc.nextInt());

if(smsi.addStudent(stu))
{
List<Student> stuList = smsi.selStudent();
for(Student st : stuList)
{
System.out.println(st.getStuName());
}

}
}
}


以前在学校写的小demo!其他的框架都是封装的JDBC!所以基础很重要,对以后学其他框架也很有帮助!