Jdbc初体验

时间:2021-04-05 17:52:59

Java数据库连接(JDBC)由一组用 Java 编程语言编写的类和接口组成。JDBC 为工具/数据库开发人员提供了一个标准的 API,使他们能够用纯Java API 来编写数据库应用程序。然而各个开发商的接口并不完全相同,所以开发环境的变化会带来一定的配置变化。本文介绍的是连接SQLServer数据库:

1.打开SQLServer的配置管理器:

Jdbc初体验

2.打开TCP/IP的属性,IPALL的端口改为1433

Jdbc初体验

3.打开服务,重新启动SQL Server(SQLEXPRESS)

Jdbc初体验

4.下面开始写简易的JDBC:

String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bookDB";
String USERNAME = "sa";
String PASSWORD = "1234";
 package com.ccec.jdbc;

 import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class jdbc {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bookDB";
private static final String USERNAME = "sa";
private static final String PASSWORD = "1234"; public boolean login(String username, String password) {
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null; try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "select userName,pwd from userInfo where userName=? and pwd=?";
st = con.prepareStatement(sql);
st.setString(1, username);
st.setString(2, password);
rs = st.executeQuery(); if (rs.next()) {
System.out.println(username);
return true;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}

5.封装后的JDBC

 package com.ccec.jdbc;

 /**
* @author StayReal
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class jdbc {
// SQLServer驱动类的全名(包名.;类名)
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// 连接的URL
private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bookDB";
// 登陆SQLServer的用户名
private static final String USERNAME = "sa";
// 登陆SQLServer的密码
private static final String PASSWORD = "1234";
// 数据库的对象声明
private Connection con = null;
private PreparedStatement pst = null;
private ResultSet rs = null; /**
* 加载驱动,建立连接
*
* @throws ClassNotFoundException
* @throws SQLException
*/
private void getConnection() throws ClassNotFoundException, SQLException {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} /**
* 执行查询
*
* @param sql
* 执行的SQL语句
* @param params
* Object数组 封装所有的SQL语句参数 顺序与SQL语句是参数顺序一致
* @return ResultSet 返回执行的结果
*/
public ResultSet execQuery(String sql, Object[] params) {
try {
getConnection();
pst = con.prepareStatement(sql);
setPrepareStatementParams(params);
rs = pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return rs;
} /**
* 执行增加、删除、修改SQL操作的方法
*
* @param sql
* 执行的参数化SQL语句
* @param params
* Object数组 封装所有的SQL语句参数 顺序与SQL语句是参数顺序一致
* @return int型 受影响的行数 -1表示出现异常
*/
public int execUpadte(String sql, Object[] params) {
try {
getConnection();
pst = con.prepareStatement(sql);
setPrepareStatementParams(params);
int affectedRows = pst.executeUpdate();
return affectedRows;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return -1;
} /**
* 为PrepareStatement设置参数
*
* @param params
* 参数数组
* @throws SQLException
*/
private void setPrepareStatementParams(Object[] params) throws SQLException {
if (params != null) {
// 传一个数组的参数
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
}
} /**
* 关闭Connection ,PrepareStatement,ResultSet
*/
private void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}