实习日记1
jdbc
1.jdbc概念
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。 有了JDBC,向各种关系数据发送SQL语句就是一件很容易的事。换言之,有了JDBC API,就不必为访问Sybase数据库专门写一个程序,为访问Oracle数据库又专门写一个程序,或为访问Informix数据库又编写另一个程序等等,程序员只需用JDBC API写一个程序就够了,它可向相应数据库发送SQL调用。同时,将Java语言和JDBC结合起来使程序员不必为不同的平台编写不同的应用程序,只须写一遍程序就可以让它在任何平台上运行,这也是Java语言“编写一次,处处运行”的优势。 简单地说,JDBC 可做三件事:与数据库建立连接、发送 操作数据库的语句并处理结果
2.jdbc的步骤
1).注册驱动class.forName(String driverName)
2).建立连接
Connection conn=DriverManager.getConnection(String url,String username,String password)
3).创建sql语句
4)获取statement对象
Statement state = con.createStatement();
或者PreparedStatement preStm = con.preparedStatement(sql);
5)执行sql语句
Result rs = state.executeQuery(sql);
int i = state.executeUpdate(sql);
或者
Result rs = preStm.executeQuery(sql);
int i=state.executeQuery(sql)
6)遍历结果集
while(rs.next()){
……
}
7)关闭连接
按顺序从内到外关
rs.close();
state.close();
con.close();
3.分层
以用户增删查改为例
1.dao
IUser:接口,里面放置对用户的抽象方法
package com.erha.dao;
import com.erha.model.User;
public interface IUser {
void insertUser(User newUser) throws Exception; // 添加用户的方法
void deleteUser(int id) throws Exception; // 删除用户的方法
void updateUser(int id,User modUser) throws Exception; // 更新用户的方法
void selectByID(int id) throws Exception; // 根据id查询用户
void selectAllUser() throws Exception;// 查询全部用户
}
2.daoimpl
userDao存放用户的增删改查
package daoimpl;
import dao.IUser;
import model.User;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao implements IUser {
DBUtil db;
Connection conn;
String sql=null;
PreparedStatement pstm;
ResultSet rs=null;
public void insertUser(User newUser) throws SQLException {
db=new DBUtil();
conn=db.getConn();
sql="insert into user_table values(?,?,?)";
pstm=conn.prepareStatement(sql);
pstm.setString(1,newUser.getId());
pstm.setString(2,newUser.getName());
pstm.setString(3,newUser.getPassword());
pstm.executeUpdate();
db.Close(pstm,rs,conn);
}
public void deleteUser(String id) throws SQLException {
db=new DBUtil();
conn=db.getConn();
sql="delete from user_table where id=?";
pstm=conn.prepareStatement(sql);
pstm.setString(1,id);
pstm.executeUpdate();
db.Close(pstm,rs,conn);
}
public void selectAll() throws SQLException {
db=new DBUtil();
conn=db.getConn();
sql="select*from user_table";
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1)+rs.getString(2)+rs.getString(3));
}
db.Close(pstm,rs,conn);
}
public void selectById(String id) throws SQLException {
db=new DBUtil();
conn=db.getConn();
sql="select*from user_table where id=?";
pstm=conn.prepareStatement(sql);
pstm.setString(1,id);
rs=pstm.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1)+rs.getString(2)+rs.getString(3));
}
db.Close(pstm,rs,conn);
}
public void changePassword(String id,String password) throws SQLException {
db=new DBUtil();
conn=db.getConn();
sql="update user_table set password=? where id=?";
pstm=conn.prepareStatement(sql);
pstm.setString(1,password);
pstm.setString(2,id);
pstm.executeUpdate();
db.Close(pstm,rs,conn);
}
public boolean getHaveUser(String name ,String password) throws SQLException {
db=new DBUtil();
conn=db.getConn();
sql="select*from user_table where name=? and password=?";
pstm=conn.prepareStatement(sql);
pstm.setString(1,name);
pstm.setString(2,password);
rs=pstm.executeQuery();
if(!rs.next()){
db.Close(pstm,rs,conn);
return false;
}
db.Close(pstm,rs,conn);
return true;
}
}
3.model
user:定义用户的属性
package model;
public class User {
private String id;
private String name;
private String password;
public User(){
}
public User(String id,String name,String password){
this.id=id;
this.name=name;
this.password=password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.Util
DBUtil:
连接数据库关闭数据库
package util;
import java.sql.*;
public class DBUtil {
private String id;
private String name;
private String password;
public Connection getConn() {
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");
}catch(Exception e){
System.out.println("数据库连接失败!");
e.printStackTrace();
}
return conn;
}
public void Close(PreparedStatement psmt, ResultSet rs, Connection conn){
try{
if(rs!=null){
rs.close();
}
if(psmt!=null){
psmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
System.out.println("数据库关闭失败!");
}
}
}
UserUtil:
定义用户的一些输入
package util;
import model.User;
import java.util.Scanner;
public class UserUtil {
Scanner sc=new Scanner(System.in);
public User getUser(){
User user=new User();
System.out.println("请输入用户学号:");
user.setId(sc.nextLine());
System.out.println("请输入用户姓名:");
user.setName(sc.nextLine());
System.out.println("请输入用户密码:");
user.setPassword(sc.nextLine());
return user;
}
public String getId(){
System.out.println("请输入用户的id");
String id=sc.nextLine();
return id;
}
}
5.service
定义登录主功能
package Service;
import daoimpl.UserDao;
import model.User;
import util.UserUtil;
import java.sql.SQLException;
import java.util.Scanner;
public class UserService {
UserDao ud;
Scanner sc;
UserUtil ut;
public boolean userLogin() throws SQLException {
ud = new UserDao();
sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
if (!ud.getHaveUser(name, password)) {
System.out.println("登录失败!用户名或则密码错误");
return false;
}
userMenu();
return true;
}
public void userMenu() throws SQLException {
while (true) {
System.out.println("请选择功能");
System.out.print("1.增加用户\n" + "2.删除用户\n" + "3.查询所有用户信息\n" + "4.查询某一用户信息\n" + "5.修改密码\n");
int choose = sc.nextInt();
sc = new Scanner(System.in);
switch (choose) {
case 1:
ut = new UserUtil();
User newUser = ut.getUser();
ud.insertUser(newUser);
break;
case 2:
System.out.println("请输入删除的学号:");
String id = sc.nextLine();
ud.deleteUser(id);
break;
case 3:
ud.selectAll();
break;
case 4:
System.out.println("请输入查询的学号:");
String id1 = sc.nextLine();
ud.selectById(id1);
break;
case 5:
System.out.println("请输入查询的学号:");
String id2 = sc.nextLine();
System.out.println("请新的密码:");
sc=new Scanner(System.in);
String password=sc.nextLine();
ud.changePassword(id2,password );
break;
}
}
}
}
6.再定义一个类,调用userService即可
4.IDEA的优化
下列图片转自实习老师