java使用jdbc连接MySQL数据库

在Java项目中使用jdbc连接数据库需要以下几个步骤:

前期准备:需要把MySQL的jar包导入到项目:

1.在项目上点击右键,new一个folder

2.在新建的folder上右键import选择file System找到jar包导入项目

3.在新导入的jar包上右键Bulid path,选择Add to Build Path

4.当出现Referenced Libraries表示添加jar包成功;

一、通过反射加载驱动,然后连接到数据库

1.确定数据库驱动

String string = "com.mysql.jdbc.Driver";//固定写法

2.加载驱动

try{

//① 加载驱动

Class.forName(string);

//②确定要连接的数据库信息

String url="jdbc:mysql://127.0.0.1:3306/xkd";//127.0.0.1表示数据库所在的PC的IP地址,3306是MySQL数据库的端口号,xkd是需要连接的数据库名称

//有些情况可能出现SSL错误,需要在xkd后面加入:?useSSL = true
String user="root";//连接数据库的用户名
String password="mysql";//连接数据库的用户名对应的密码

//③建立连接

Connection connection=  DriverManager.getConnection(url,user,password);

二、操作数据库

//① 获取statement
Statement statement=connection.createStatement();
//②确定SQL语句
String  sqlString="insert into student(id,name,pass) values(2,'张同学','password')";
//③执行操作
statement.executeUpdate(aqlString);
三、关闭资源
statement.close();
 connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("找不到类");
} catch (SQLException e) {
e.printStackTrace();
}

package testio;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Data {
   /**
    *   JDBC
    * 
    * */
	public static void main(String[] args) {
		//1、连接数据库   反射
		    //① 确定数据库驱动 MySql
		     String  string="com.mysql.jdbc.Driver";
		    try {
		    //② 加载驱动	
				Class.forName(string);			
			//③确定要连接的数据库、用户名、密码
				String url="jdbc:mysql://127.0.0.1:3306/xkd";
				String user="root";
				String password="mysql";
			//④建立与数据库的连接
				Connection connection=  DriverManager.getConnection(url,user,password);
		//2、操作数据库
			//① 获取statement
				Statement statement=connection.createStatement();
			//②确定SQL语句
				String  aqlString="insert into student(id,name,pass) values(2,'张同学','password')";
			//③执行操作
			        statement.executeUpdate(aqlString);
		//3、关闭资源
				         statement.close();
				         connection.close();
		    } catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("找不到类");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值