前言
记录jdbc学习笔记,书上写的很草率,还是得听课。。。
一、jdbc连接
连接执行步骤
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class test {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost:3306/StudentManage";
String user = "root";
String password = "密码";
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection con = DriverManager.getConnection(url,user,password);
//3.定义sql语句
String sql = "insert into studentdata(id, name, age, specialty)\n" +
"values(032005108,'lihua',18,'数计')";
//4.获取执行sql的对象 Statement
Statement st = con.createStatement();
//5.执行sql
int count = st.executeUpdate(sql);
//6.处理结果
System.out.println(count);
//7.释放资源
st.close();
con.close();
}
}
二、对象讲解
1.DriverManager
驱动管理对象
功能:
1.注册驱动:告诉程序该使用哪一个数据库驱动jar
static void registerDriver(Driver driver)
注册与给定的驱动程序
2.获取数据库连接
Connection con = DriverManager.getConnection(url,user,password);
注意:
mysql5之后可不写。。。但最好还是写上
2.Connection
一.功能:
1.获取执行sql对象
statement createStatement()
PreparedStatement prepareStatement(String sql)
二.管理事务:
*开启事务:void setAutoCommit(boolean autoCommit)
*提交事务:commit()
*回滚事务:rollback()
3.Statement
一.执行sql
1.boolean execute(string sql): 可以执行任意的sql
2.int executeUpdate(String sql) : 执行DML DDL
*返回值:影响的行数,可以通过影响的行数判断DML语句是否执行成功,返回值大于0则运行成功,反之,则失败
3.ResultSet executeQuery(String sql) : 执行DQL (select)语句
小练习:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class testInesrt {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
String url = "jdbc:mysql://localhost:3306/StudentManage";
String user = "root";
String password = "xxxxxxxxxx";
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String sql = "insert into studentdata(id, name, age, specialty)\n" +
"values(032005101,'陈黾杰',18,'数计')";
statement = connection.createStatement();
int count = statement.executeUpdate(sql);
if (count>0){
System.out.println("添加成功!");
}
else{
System.out.println("添加失败!");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
4.Resultset
结果集对象,封装查询结果
*boolean next():游标向下移动一行,判断是最后一行末尾(是否有数据),如果是则返回flase,后则返回true
*getXxx():获取数据(Xxx数据类型) 如: getInt() getString()
*参数:
1.Int:代表列的编号。如 :getString(1)
2.String: 代表列的名称
注意:
使用步骤:
1.游标向下移动一行
2.判断是否有数据
3.获取数据
练习
import java.sql.*;
public class testResult {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try {
String url = "jdbc:mysql://localhost:3306/StudentManage";
String user = "root";
String password = "xxxxxxxxx";
Class.forName("com.mysql.jdbc.Driver");
String sql = "select * from studentdata";
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
result = statement.executeQuery(sql);
while (result.next()) {
int id = result.getInt(1);
String name = result.getString("name");
String specialty = result.getString("specialty");
System.out.println(id + "--" + name + "--" + specialty);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (result != null) {
try {
result.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
三、封装练习
将表封装成类
学生类
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCutils {
private static String url;
private static String user;
private static String password;
private static String Driver;
//文件的读取,只需一次即可拿到这些值。使用静态代码块
static {
//读取资源文件,获取值
try {
//1.创建Properties集合类。
Properties pro = new Properties();
//获取src路径下文件的方式 ---》ClassLoader 类加载器
ClassLoader classLoader = JDBCutils.class.getClassLoader();
URL rs = classLoader.getResource("jdbc.properties");
String path = rs.getPath();
//2.加载文件
pro.load(new FileReader(path));
//3.获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
Driver = pro.getProperty("Driver");
Class.forName(Driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection connection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//两个捕获不能合并 , 合并后另一个不会执行
public static void close(Statement st, Connection con) {
if (st != null) {
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null)
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(Statement st, Connection con, ResultSet result) {
if (st != null) {
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null)
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (result != null)
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
创建jdbc.properties文件
url = jdbc:mysql://localhost:3306/StudentManage
user = root
password = 20010615haorun
Driver = com.mysql.jdbc.Driver
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。