工具:IDEA 2018.2.2 mysql5.5 SQLyog
将下载好的驱动解压,然后在java文件中新建文件lib中,然后将解压好的驱动添加到lib文件夹中。
将添加的
//2创建连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test8?useSSL=true&characterEncoding=utf-8&user=root&password=1234");
//3.写sql
String sql = "select * from user ";
//4.得到statement对象
st = connection.prepareStatement(sql);
//5.执行sql
rs = statement.executeQuery();
//6处理结果集
while (resultSet.next()){
System.out.print(" id: "+resultSet.getInt(1));
System.out.print("name: "+resultSet.getString(2));
System.out.print(" password: "+resultSet.getString(3));
System.out.println("-----------");
}
//7关闭资源
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
package dbo;
import java.sql.*;
public class DB {
public static void main(String[] args) {
ResultSet rs=null;
PreparedStatement st=null;
Connection con=null;
try {
//1加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2创建连接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test8?useSSL=true&characterEncoding=utf-8&user=root&password=1234");
//3.写sql
String sql="select * from username";
//4.得到statement对象
st = con.prepareStatement(sql);
//5.执行sql
rs = st.executeQuery();
//6处理结果集
while (rs.next()){
System.out.print(" id: "+resultSet.getInt(1));
System.out.print(" name: "+resultSet.getString(2));
System.out.print(" password: "+resultSet.getString(3));
System.out.println("--------");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//7关闭资源
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
将数据库的连接和关闭封装
private int id;
private String name;
private String password;
public int getid(){
return id;
}
public String getname(){
return name;
}
public String getpassword(){
return password;
}
public void setid(int id){
this.id=id;
}
public void setname(String name){
this.id=id;
}
public void setpassword(String password){
this.id=id;
}
public Userinfo(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
@Override
public String toString() {
return "Userinfo{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public static Connection getConnection(){
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test1?useSSL=true&characterEncoding=utf-8&user=root&password=1234");
return con;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void closeDB(Connection con,PreparedStatement st,ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class jdbc {
public List find() {
List list =new ArrayList<>();
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
con= dbl.getConnection();
String sql = "select * from user ";
st = con.prepareStatement(sql);
rs = st.executeQuery();
while (rs.next()) {
int id=rs.getInt(1);
String name=rs.getString(2);
String password=rs.getString(3);
Userinfo userinfo=new Userinfo(id,name,password);
userinfo.setid(id);
list.add(userinfo);
}
System.out.println(list.toString());
} catch (Exception e) {
e.printStackTrace();
//System.out.println("数据库连接失败" + e.getMessage());
} finally {
dbl.closeDB(con,st,rs);
}
return list;
}
public static void update(){
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
con= dbl.getConnection();
String sql = "update user set name ='FFFF' where id='1' ";
st = con.prepareStatement(sql);
st.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
//System.out.println("数据库连接失败" + e.getMessage());
} finally {
dbl.closeDB(con,st,rs);
}
}
public static void update(){
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
con= dbl.getConnection();
String sql = "update user set name ='FFFF' where id='1' ";
st = con.prepareStatement(sql);
st.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
//System.out.println("数据库连接失败" + e.getMessage());
} finally {
dbl.closeDB(con,st,rs);
}
}
public static void insert(){
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
con= dbl.getConnection();
String sql = "insert into user values (4,'小明','8757')";
st = con.prepareStatement(sql);
st.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
//System.out.println("数据库连接失败" + e.getMessage());
} finally {
dbl.closeDB(con,st,rs);
}
}