快速入门 (1)导入驱动jar包 ①复制jar到项目的libs(自己建立方便管理)文件夹里 ②右键,点击Add as Library (2)注册驱动 (3)获取数据库连接对象 Connection (4)定义sql (5)获取执行sql语句的对象 Statement (6)执行sql,接收返回结果 (7)处理结果 (8)释放资源 代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo1 {
public static void main(String[] args) throws Exception{
//(2)注册驱动
Class.forName("com.mysql.jdbc.Driver");
//(3)获取数据库连接对象 Connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","root");
//(4)定义sql
String sql = "update student set s_name='gb' where s_id = 1";
//(5)获取执行sql语句的对象 Statement
Statement stmt = conn.createStatement();
//(6)执行sql,接收返回结果result
int result = stmt.executeUpdate(sql);
//(7)处理结果
System.out.println(result);
//(8)释放资源
stmt.close();
conn.close();
}
}
结果:
输出了 1