java连接mysql数据库(JDBC)
一、使用步骤
1.连接数据库
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/Flower";
String name = "root";
String pwd = "root";
Connection connection = DriverManager.getConnection(url,name,pwd);
//获取statement对象
/*Statement statement = connection.createStatement();*/
String sql = "select * from flower";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//执行语句
/*ResultSet resultSet = statement.executeQuery(sql);*/
ResultSet resultSet = preparedStatement.executeQuery(sql);
2.读取数据
while (resultSet.next()){
System.out.print("[");
System.out.print( resultSet.getInt("id")+"\t");
System.out.print( resultSet.getString("name")+"\t");
System.out.print( resultSet.getInt("price")+"\t");
System.out.print( resultSet.getString("production"));
System.out.println("]");
}
总结
Java连接数据库的步骤
1.加载数据库驱动
(1)先将数据库驱动添加到项目文件中
(2)加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
2.创建数据库连接
String url = "jdbc:mysql://localhost:3306/Flower";
String name = "root";
String pwd = "root";
Connection connection = DriverManager.getConnection(url,name,pwd);
3.创建数据库操作语句执行对象
statement对象和preparedstatement对象都可以,preparedstatement是statement的子类,都返回result对象集合
preparedstatement类提供了get和set方法,可以通过生成的result对象集合对插入语句的数据值进行赋值或者打印查询结果
//获取statement对象
/*Statement statement = connection.createStatement();*/
String sql = "select * from flower";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//执行语句
/*ResultSet resultSet = statement.executeQuery(sql);*/
ResultSet resultSet = preparedStatement.executeQuery(sql);
4.关闭资源
先打开的后关闭
if (resultSet != null){
resultSet.close();
}
if(preparedStatement != null){
preparedStatement.close();
}
if(connection != null)
{
connection.close();
}
所有代码展示
package com.company.jdbc;
import javafx.scene.control.SplitPane;
import java.awt.*;
import java.sql.*;
public class test1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/Flower";
String name = "root";
String pwd = "root";
Connection connection = DriverManager.getConnection(url,name,pwd);
//获取statement对象
/*Statement statement = connection.createStatement();*/
String sql = "select * from flower";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//执行语句
/*ResultSet resultSet = statement.executeQuery(sql);*/
ResultSet resultSet = preparedStatement.executeQuery(sql);
//返回地数据存储在result中
while (resultSet.next()){
System.out.print("[");
System.out.print( resultSet.getInt("id")+"\t");
System.out.print( resultSet.getString("name")+"\t");
System.out.print( resultSet.getInt("price")+"\t");
System.out.print( resultSet.getString("production"));
System.out.println("]");
}
if (resultSet != null){
resultSet.close();
}
if(preparedStatement != null){
preparedStatement.close();
}
if(connection != null)
{
connection.close();
}
}
}