java连接数据库Java DataBase Connectivity
大概分为四个步骤
第一步:导入驱动,加载具体的驱动类
第二步:与数据库建立连接
第三步:发送SQl,执行(如果是查询,就会出现返回的结果,其他的没有)
*第四步:接受返回的结果
会出现两个异常(SQLException ClassNotFoundException )
一.导入驱动(需要下载)
二.加载具体的驱动类Class.forName("com.mysql.jdbc.Driver");
三.与数据库建立连接
private static final String url="jdbc:mysql://47.114.87.54:3306/pet";//连接字符串
private static final String username="pet_reader";//用户名
private static final String password="pet@123"; //密码
connection = (Connection) DriverManager.getConnection(url,username,password);
这里连接的是云服务器,如果要连接到自己的电脑上
private static final String url="jdbc:mysql://localhost:3306/数据名";//连接字符串
四.发送SQl,执行
stmt = (Statement) connection.createStatement();
String sql="insert into goods values(1,'2',1)";
如果是,删除,修改的话,就会返回有多少条数,删除,修改
int count = stmt.executeUpdate(sql);//返回值表示增删改几条数据
*五.如果是查询,返回的结果集用ResultSet来接收,用循环来输出结果
ResultSet rs=null;
rs= (ResultSet) stmt.executeQuery(sql);
while(rs.next())
{
//rs是一行的结果,如果字段类型为整形,就用getInt("字段名"),来获取,并且返回的结果也为整形
//如果字段的字符串类型,就用getString("字段名");并且返回的结果也为字符串
}
import java.awt.List;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import sun.*;
import java.util.*;
public class JDBCDemo {
private static final String url="jdbc:mysql://47.114.87.54:3306/pet";//连接字符串
private static final String username="pet_reader";//用户名
private static final String password="pet@123"; //密码
//增删改
public static void update() throws ClassNotFoundException {
Connection connection=null;
Statement stmt=null;
try {
//a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//b.与数据库建立连接
connection = (Connection) DriverManager.getConnection(url,username,password);
//c.发送SQl,执行
stmt = (Statement) connection.createStatement();
String sql="insert into goods values(1,'2',1)";
int count = stmt.executeUpdate(sql);//返回值表示增删改几条数据
if(count>0)
{
System.out.println("增删改成功");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
stmt.close();
connection.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
//查询
public static ArrayList<Pet> query() throws ClassNotFoundException {
Connection connection=null;
Statement stmt=null;
ResultSet rs=null;
try {
//a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//b.与数据库建立连接
connection = (Connection) DriverManager.getConnection(url,username,password);
//c.发送SQl,执行
stmt = (Statement) connection.createStatement();
String sql="select * from pet";
rs= (ResultSet) stmt.executeQuery(sql);//返回值表示增删改几条数据
while(rs.next())
{
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
rs.close();
stmt.close();
connection.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
return list;
}
public static void main(String[] args) throws ClassNotFoundException
{
// update();
query();
System.out.print("成功");
}
}