jdbc-手写Java方法连接数据库

一、关键四元素

     ①    com.mysql.jdbc.Driver      mysql数据库连接jar包。   获取途径: 链接:https://pan.baidu.com/s/1SFcjuut5BsO-4x3U-SdG6Q        提取码:aafz   

  ②    private static final String URL = "jdbc:mysql://localhost:3306/zhaowejie?" + "useUnicode=true&characterEncoding=UTF8";   这是数据库的连接路径。

               “jdbc”是协议开头。

               “mysql”是子协议数据库名称,代表数据库是mysql。

               “//localhost:3306”是服务器的IP地址和端口,“zhaowejie?”代表你要连接得数据库的名字。

               "useUnicode=true&characterEncoding=UTF8" 指定操作数据库使用utf-8字符编码进行操作。

  ③   private static final String USER = "root";    数据库连接名。

  ④   private static final String USER = "root";    数据库连接密码。

 

二、代码实现

  

package machine;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTest {

// 数据库连接ip,user,password需要修改
// private static final String
       private static final String URL = "jdbc:mysql://localhost:3306/zhaowejie?"+ "useUnicode=true&characterEncoding=UTF8";
       private static final String USER = "root";
  private static final String PASSWORD = "123456";

 

public static void main(String[] args) {

  Connection conn = null;
     Integer id = 0;
  String userName = null;
  String userPassWord = null;
try {
  // 1.加载驱动程序
  Class.forName("com.mysql.jdbc.Driver");
  // 2.获得数据库的连接
  conn = DriverManager.getConnection(URL, USER, PASSWORD);
  // 3.通过数据库的连接操作数据库,实现增删改查(使用Statement类)
  query(conn, 1);
  insert(conn, 3, "zhaoweijie", "123445");
  update(conn, "ws", "23456", 1);
} catch (ClassNotFoundException e) {
  e.printStackTrace();
} catch (SQLException e) {
  e.printStackTrace();
}
}

// 查询
private static void query(Connection conn, Integer id) throws SQLException {
  String sql = "SELECT id, userName , userPassWord FROM USERS WHERE id = ?";
  PreparedStatement ps = null;
  ResultSet rs = null;
try {
  ps = conn.prepareStatement(sql);
  ps.setInt(1, id);
  rs = ps.executeQuery();
  while (rs.next()) {
  id = rs.getInt(1);
  String userName = rs.getString(2);
  String userPassWord = rs.getString(3);
  System.out.println("id=" + id + "userName=" + userName + "userPassWord=" + userPassWord);
}
} catch (SQLException e) {
  e.printStackTrace();
} finally {
  rs.close();
conn.close();
  ps.close();
}
}

  // insert
  private static void insert(Connection conn, Integer id, String userName, String userPassWord) throws SQLException {

  int i = 0;
  // sql 插入的语句
  String s = "" + "insert into users(id,userName,userPassWord) values(?,?,?)";
  PreparedStatement ps = null;

  // ResultSet rs=null;
try {
  ps = conn.prepareStatement(s);
  ps.setInt(1, id);
  ps.setString(2, userName);
  ps.setString(3, userPassWord);
  i = ps.executeUpdate();
} catch (SQLException e) {
  e.printStackTrace();
} finally {
  // rs.close();
  conn.close();
  ps.close();
}
}

  // update
  private static void update(Connection conn, String userName, String userPassword, Integer id) throws SQLException {
  int i = 0;
  String sql = "update users set userName = " + "'" + userName + "'" + "," + "userPassword = " + "'"+ userPassword + "'" + " " + "where id = " + id;
  Statement ps = null;

  // ResultSet rs=null;
try {
  // ps = conn.prepareStatement(sql);
  ps = conn.createStatement();
  i = ps.executeUpdate(sql);
} catch (SQLException e) {
  e.printStackTrace();
} finally {
  // rs.close();
  conn.close();
  ps.close();
}
}
}

 

   

posted @ 2018-11-04 19:44  雪呐江的夜  阅读(1517)  评论(0)    收藏  举报