


DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName(“com.mysql.jdbc.Driver”);
DriverManager.getConnection(“jdbc:mysql://数据库地址:端口号/数据库名”,”用户名”, “密码”);
Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”,”root”, “test”);
Statement statement = connection.createStatement();
| 方法 | 方法描述 |
|---|---|
| execute(String sql ) | 该方法可以执行任意SQL语句。当且仅当执行select语句且有返回结果时该方法返回true, 其他情况下该方法返回false |
| executeUpdate(String sql ) | 该方法常用于执行DML( INSERT、UPDATE或DELETE)和DDL语句。执行DML语句时返回受SQL语句影响的行数,执行DDL语句时返回0 |
| executeQuery( String sql) | 该方法通常执行査询语句,执行后返回代表结果集的ResultSet对象 |
ResultSet resultSet = statement.executeQuery(sql);
ResultSet resultSet = statement.executeQuery(sql);
| 方法 | 方法描述 |
|---|---|
| boolean next() | 将游标从当前位置向下移动一行 |
| boolean previous() | 将游标从当前位置向上移动一行 |
| void afterLast() | 将光标移动到末尾,正好位于最后一行之后 |
| void beforeFirst() | 将光标移动到开头,正好位于第一行之前 |
| Object getObject(int columnIndex) | 根据序号取值,索引从1开始 |
| Object getObject(String ColomnName) | 根据列名取值 |
| int getInt(int colIndex) | 以int形式获取ResultSet结果集当前行指定列号值 |
| int getInt(String colLabel) | 以int形式获取ResultSet结果集当前行指定列名值 |
| float getFloat(int colIndex) | 以float形式获取ResultSet结果集当前行指定列号值 |
| float getFloat(String colLabel) | 以float形式获取ResultSet结果集当前行指定列名值 |
| String getString(int colIndex) | 以String形式获取ResultSet结果集当前行指定列号值 |
| String getString(String colLabel) | 以String形式获取ResultSet结果集当前行指定列名值 |
| Date getDate(int columnIndex) | 以Date形式获取ResultSet结果集当前行指定列号值 |
| Date getDate(String columnName) | 以Date形式获取ResultSet结果集当前行指定列号值 |
| void close() | 关闭ResultSet对象 |
| Java数据类型 | 数据库的数据类型 |
|---|---|
| byte | tityint |
| int | int |
| short | smallint |
| long | bigint |
| float | float |
| double | double |
| String | char/varchar |
| Date | date |
-- 若存在数据库test则删除
DROP DATABASE IF EXISTS test;
-- 创建数据库test
CREATE DATABASE test;
-- 选择数据库test
USE test;
-- 创建学生表
CREATE TABLE student (
studentid INT,
studentname VARCHAR(50)
);
-- 向学生表插入数据
INSERT INTO student (studentid,studentname) VALUES (1,"lili");
INSERT INTO student (studentid,studentname) VALUES (2,"dodo");
INSERT INTO student (studentid,studentname) VALUES (3,"tutu");
INSERT INTO student (studentid,studentname) VALUES (4,"mqmq");
INSERT INTO student (studentid,studentname) VALUES (5,"pmpm");
package cn.com.demo1;
public class Student {
private int studentID;
private String studentName;
public Student() {
}
public Student(int studentID, String studentName) {
this.studentID = studentID;
this.studentName = studentName;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public String toString() {
return "Student [studentID=" + studentID + ", studentName=" + studentName + "]";
}
}
package cn.com.demo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
//创建Statement
statement = connection.createStatement();
String sql = "select * from student";
//执行SQL
resultSet = statement.executeQuery(sql);
//处理结果
while (resultSet.next()) {
Student student = new Student();
int id = resultSet.getInt("studentid");
String name = resultSet.getString("studentname");
student.setStudentID(id);
student.setStudentName(name);
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
}
package cn.com.demo2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
//创建Statement
statement = connection.createStatement();
//依据姓名查找学生信息
String studentname="lili";
String sql = "select * from student where studentname="+"'"+studentname+"'";
System.out.println(sql);
//执行SQL
resultSet = statement.executeQuery(sql);
//处理结果
while (resultSet.next()) {
Student student = new Student();
int id = resultSet.getInt("studentid");
String name = resultSet.getString("studentname");
student.setStudentID(id);
student.setStudentName(name);
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
statement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
}
String studentname="abc' or '1=1";
package cn.com.demo3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestJDBC {
public static void main(String[] args) {
TestJDBC jdbcDemo=new TestJDBC();
jdbcDemo.addStudent();
jdbcDemo.deleteStudent();
jdbcDemo.updateStudent();
jdbcDemo.findStudent();
}
//添加学生
private void addStudent() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
Student student=new Student(6, "zczc");
String sql = "insert into student values (?,?)";
//创建PrepareStatement
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, student.getStudentID());
preparedStatement.setString(2, student.getStudentName());
//执行SQL
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
//删除学生
public void deleteStudent() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
String studentname="zczc";
String sql = "delete from student where studentname=?";
//创建PrepareStatement
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, studentname);
//执行SQL
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
//修改学生
private void updateStudent() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
int studentid=1;
String studentname="hoho";
String sql = "update student set studentname=? where studentid=?";
//创建PrepareStatement
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, studentname);
preparedStatement.setInt(2, studentid);
//执行SQL
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
//查找学生
private void findStudent() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
//依据姓名查找学生信息
String studentname="lili";
String sql = "select * from student where studentname=?";
//创建PrepareStatement
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, studentname);
//执行SQL
resultSet = preparedStatement.executeQuery();
//处理结果
while (resultSet.next()) {
Student student = new Student();
int id = resultSet.getInt("studentid");
String name = resultSet.getString("studentname");
student.setStudentID(id);
student.setStudentName(name);
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
}
atch (Exception e) {
e.printStackTrace();
//关闭资源
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
resultSet = null;
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
preparedStatement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}
}
}
}