JDBC 初始。

package cn.zhouzhou;
/*
一、JDBC?
1.(java date base connectivity,java)是一种用于执行SQL语句的java API 。
2.jdbc本质是一套API ,由开发公司定义的类和接口。
3.使用mysql驱动,是一套类库,实现了接口
4.驱动程序类库,实现接口的重写方法,有驱动程序操作数据库。 二、jdbc怎么操作?
1.注册驱动,获得连接。
导入jar包 自己下载,我的是mysql-connector-java-5.1.37-bin.jar
2.获得语句执行平台,就可以执行SQL语句
3.处理结果,释放资源! */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; public class java连接数据库 {
public static void main(String[] args)throws Exception {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库连接
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/qy66","root","root");
//3.使用SQL语句 定义!
String sql="select *from shop";
//4.获得执行SQL语句对象 statement
Statement stat =con.createStatement();
//5.执行SQL语句
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("id")+rs.getString("name")+rs.getString("price"));
} //6.处理结果
//7.释放资源
stat.close();
con.close();
}
private static void show()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/qy66","root","root");
String sql="update shop set name='李华' where id=10";
Statement stat =con.createStatement();
int i= stat.executeUpdate(sql);
System.out.println(i);
stat.close();
con.close();
}
private static void run()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/qy66","root","root");
String sql="update shop set name='001'where id=8";
Statement stat=con.createStatement();
int i =stat.executeUpdate(sql);
System.out.println(i);
stat.close();
con.close(); }
private static void run01()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc.mysql://localhost:3306/qy66","root"," root");
String sql="update shop set name='dd' where id=78";
Statement stat =con.createStatement();
int i =stat.executeUpdate(sql);
System.out.println(i);
stat.close();
con.close();
}
private static void run02() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc://mysql://localhost:3306/qy66","root","root");
String sql="update shop set naem'23' where id=78";
Statement start =con.createStatement();
int i = start.executeUpdate(sql);
System.out.println(i);
start.close();
con.close(); }
private static void run03() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/qy66");
String sql="update shop set name='45' where id=45";
Statement start=con.createStatement();
int i=start.executeUpdate(sql);
System.out.println(i);
start.close();
con.close();
}
}
上一篇:Your Customers Do Not Mean What They Say


下一篇:C#小知识点记录(QQ交流群的一个小问题)Linq提取数据