Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcDemo {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
String url = "jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
//JDBC jar包与mysql版本不兼容时用useUnicode=true&characterEncoding=utf-8&useSSL=false
String username = "root";
String password = "123456";
Connection con = DriverManager.getConnection(url, username, password);
//定义SQL语句
String Sql =" UPDATE account set money = 2500 WHERE name ='张三'";
//获取执行SQL的对象statement
Statement statement = con.createStatement();
//执行SQL语句(返回受影响的行数)
int count = statement.executeUpdate(Sql);
//处理结果
System.out.println(count);
con.close();
statement.close();
}
}