关于mysql,sqlserverl,与oracle数据库连接总结
2024-09-02 19:42:50
首先准备工具类,其中需要修改的地方分别做标注
代码一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;
}
最新文章
- iOS开发之XCode设置--消除AFN的警告
- Core Data 使用映射模型
- PAT-乙级-1026. 程序运行时间(15)
- Python新手学习基础之运算符——成员运算与身份运算
- js函数设计原则
- 我的Python成长之路---第三天---Python基础(9)---2016年1月16日(雾霾)
- 进入MAC下面的Library目录
- tomcat path设置
- JSP 获得Spring 注射对象
- 关于使用scrapy框架编写爬虫以及Ajax动态加载问题、反爬问题解决方案
- 高效求a的n次幂的算法
- appium sendkeys 输入数字丢失问题
- 转://Linux下tmpfs介绍及使用
- Composite组合模式(结构型模式)
- Linux磁盘IO监控[zz]
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
- linux查看tomcat日志
- win7 免安装MariaDB
- POJ 2987 Firing(最大流最小割の最大权闭合图)
- jquery清空form表单