JDBC第一种源码:
package com.hujin;
import java.sql.*;
public class jdbcDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");//固定写法
//2.用户信息和url
String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSl=true";//固定写法
String username="root";
String password="root";
//3.连接成功,数据库对象
Connection connection = DriverManager.getConnection(url, username, password);
//4.执行SQL的对象
Statement statement = connection.createStatement();
//5.执行SQL的对象 查看返回结果
String sql="SELECT * FROM student";
//int result = statement.executeUpdate(sql);//这个语句方法是用在对数据库发生了增删改之后用
ResultSet resultSet=statement.executeQuery(sql);//这个语句方法是用在对数据库发生了查询之后用,一般是查询,一般用这个方法
while (resultSet.next()){
System.out.println("id="+resultSet.getObject("id"));
System.out.println("name="+resultSet.getObject("name"));
}
//6.释放自由
resultSet.close();
statement.close();
connection.close();
}
}
JDBC第二种源码:
public class demo{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");//固定写法
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSl=true";//固定写法
String username = "root";
String password = "root";
//3.连接成功,数据库对象
Connection connection = DriverManager.getConnection(url, username, password);
String sql="select * from school.student";
PreparedStatement preparedStatement=connection.prepareStatement(sql);
ResultSet resultSet=preparedStatement.executeQuery();//这个方法是sql语句用来查询了之后用的
//preparedStatement.executeUpdate();这个方法是sql语句用来增删改了之后用的
while (resultSet.next()){
Integer id =resultSet.getInt(1);
String name=resultSet.getString(2);
System.out.println(id);
System.out.println(name);
}
connection.close();
preparedStatement.close();
resultSet.close();
}
JDBC第三种(Ajax中的JDBC):
package com.hujin.dao;
import com.hujin.daomian.City;
import com.hujin.daomian.Province;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Class.forName;
public class queryDao {
private Connection conn;
private PreparedStatement pst;
private ResultSet rs;
private String sql;
private String url="jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=utf8&useSSl=true";
private String username="root";
private String password="root";
// 查询所有的省份信息
public List<Province> queryProvinceList(){
List<Province> provinces=new ArrayList<>();
try{
Province p=null;
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection(url,username,password);
sql="select * from pro order by id";
pst=conn.prepareCall(sql);
rs=pst.executeQuery();
while (rs.next()){
p=new Province();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setJiancheng(rs.getString("jiancheng"));
p.setShenghui(rs.getString("shenghui"));
provinces.add(p);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
if (rs !=null){
rs.close();
}if (conn!=null){
conn.close();
}
if (pst!=null){
pst.close();
}
}catch(Exception exception){
exception.printStackTrace();
}
}
return provinces;
}
//查询省份下面的城市,方法中给个参数,为了下面查询语句中的?联合
public List<City> queryCityList(Integer provinceId){
List<City> cities=new ArrayList<>();
try{
City city=null;
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection(url,username,password);
sql="select * from city where provinceid=?";
pst=conn.prepareCall(sql);
// 设置省份的参数值,通过调用方法之后的参数来查询
pst.setInt(1,provinceId);
rs=pst.executeQuery();
while (rs.next()){
city=new City();
city.setId(rs.getInt("id"));
city.setName(rs.getString("name"));
cities.add(city);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
if (rs !=null){
rs.close();
}if (conn!=null){
conn.close();
}
if (pst!=null){
pst.close();
}
}catch(Exception exception){
exception.printStackTrace();
}
}
return cities;
}
}
JDBC驱动包:
链接:https://pan.baidu.com/s/14JGI4Fm5P8HHG8ATW16ovA
提取码:y04e
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)