首先准备工具类,其中需要修改的地方分别做标注

代码一sqlserver为例,不同数据库只需修改我所标记的第一处和第二处

mysql     第一处应为:com.mysql.jdbc.Driver   第二处url 为:jdbc:mysql://127.0.0.1:3306/javaweb    //3306位端口,javaweb为数据库名

Oracle   第一处应为:oracle.jdbc.OracleDriver  第二处url 为:jdbc:oracle:thin:@localhost:1522:orcl   //1522为端口,orcl为数据库名

sql语句因数据库不同略有差异,但都大同小异。

package Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBUtil {
public static Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); //1.此处根据数据库修改
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "sa";        //用户名
String password = "123456"; //密码
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=keji"; //2.数据库名
Connection connection = null;
try {
//2 �������Ӷ���connection
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection; }
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

对数据库操作代码

简单的插入

public void add (User user){
Connection connection=DBUtil.getConnection();
String sql = "select count(*) from t_user where username = ?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getUsername());
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
if (resultSet.getInt(1) > 0) {
throw new UserException("用户名存在") ;
}
} sql = "insert into t_user(username,password) values (?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}

删除

    public void delete(String username) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection();
String sql = "delete from t_user where username = ?";
PreparedStatement preparedStatement = null; try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} }

修改

    public void update(User user) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection();
//׼��sql���
String sql = "update t_user set password = ? where username = ?";
//������䴫�����
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getPassword());
preparedStatement.setString(2, user.getUsername());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} }

查询

    public User load(String username) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection();
//׼��sql���
String sql = "select * from t_user where username = ?";
//������䴫�����
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setUsername(username);
user.setBalance(resultSet.getFloat("balance"));
user.setPassword(resultSet.getString("password"));
user.setType(resultSet.getInt("type"));
user.setCredit(resultSet.getFloat("credit"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return user;
}

最新文章

  1. iOS开发之XCode设置--消除AFN的警告
  2. Core Data 使用映射模型
  3. PAT-乙级-1026. 程序运行时间(15)
  4. Python新手学习基础之运算符——成员运算与身份运算
  5. js函数设计原则
  6. 我的Python成长之路---第三天---Python基础(9)---2016年1月16日(雾霾)
  7. 进入MAC下面的Library目录
  8. tomcat path设置
  9. JSP 获得Spring 注射对象
  10. 关于使用scrapy框架编写爬虫以及Ajax动态加载问题、反爬问题解决方案
  11. 高效求a的n次幂的算法
  12. appium sendkeys 输入数字丢失问题
  13. 转://Linux下tmpfs介绍及使用
  14. Composite组合模式(结构型模式)
  15. Linux磁盘IO监控[zz]
  16. python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
  17. linux查看tomcat日志
  18. win7 免安装MariaDB
  19. POJ 2987 Firing(最大流最小割の最大权闭合图)
  20. jquery清空form表单

热门文章

  1. selenium+java:获取列表中的值
  2. PJzhang:URL重定向漏洞的72般变化
  3. eclipse 导入项目出现的问题汇总
  4. 前端实现app引导页面动画效果
  5. CDH安装前系统优化准备
  6. [转帖]Unix版权史
  7. elasticsearch教程--中文分词器作用和使用
  8. idea - maven子工程找不到父工程pom
  9. idea中模块累积编写
  10. 02:linux常用命令