陕西省住房和城乡建设厅综合服务网站,上海百度整站优化服务,怎样做网站赚流量,南昌集团制作网站开发一#xff1a;JDBC sun#xff1a;提供了一套通用性的接口#xff1a;可以连接任何的数据库#xff1a; 连接数据库的具体得到实例#xff0c;具体的数据库厂商实现的。 连接数据的步骤#xff08;别忘了复制jar包#xff09;:( 1#xff09;注册驱动#xff…一JDBC sun提供了一套通用性的接口可以连接任何的数据库 连接数据库的具体得到实例具体的数据库厂商实现的。 连接数据的步骤别忘了复制jar包:( 1注册驱动 Class.forNameDriverManager 2获得链接对象Connection 3创建sql容器语句 4执行sql语句:: stmt 5查询操作遍历结果集ResultSet 6关闭资源 ResultSet详解更多可以看API 封装了结果集的对象内部有一个可移动的光标默认情况指向第一条记录集的上一条记录 next;光标下移动一次返回的boolean的值;判断是否有结果可以被遍历 previous;光标上移动一次 last移动到最后一行 afterLast;移动到最后一行之后 beforeFirst移动到第一行的之前 first getObject; getInt; 的GetString; getFloat; getDouble getDate; getXxx 的的getMetaData获得结果集的元数据:(重要
package com.yidongxueyuan.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class StudentDaoImpl {public static void main(String[] args) {try {Class.forName(com.mysql.jdbc.Driver);Connection conn DriverManager.getConnection(jdbc:mysql://localhost:3306/jdbc01, root, root);Statement stmt conn.createStatement();String sql select * from student ;ResultSet rs stmt.executeQuery(sql);/*while(rs.next()){int id rs.getInt(sid);String name rs.getString(sname);System.out.println(id name);}*/rs.next(); //上移动一次
// rs.previous();// rs.last();
// rs.first() ;rs.beforeFirst();while(rs.next()){
// Object object rs.getObject(1);int id rs.getInt(sid);String name rs.getString(sname);System.out.println(id name);}rs.close(); stmt.close(); conn.close();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} finally{}}
}// mysql封装工具类
封装好的UTIL
package com.yidongxueyuan.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/** 对JDBC 进行封装 */
public class JdbcUtils2 {//准备数据库的四大参数 private static final String driver com.mysql.jdbc.Driver; private static final String url jdbc:mysql://localhost:3306/jdbc01; private static final String username root; private static final String password root;/** 驱动 只需要注册一次就OK * */static{try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}/** 1创建工具类 直接获得一个连接对象 */public static Connection getConnection (){try {Connection connection DriverManager.getConnection(url, username, password);return connection; } catch (SQLException e) {throw new RuntimeException(e);}}/** 定义一个方法 释放资源 直接将rs stmt conn 全部释放 */public static void release(ResultSet rs ,Statement stmt , Connection conn){if(rs!null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}rsnull;}if(stmt!null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}stmtnull;}if(conn!null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}connnull;}}}调用工具类实现操作
package com.yidongxueyuan.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/** 使用工具类 对代码进行重构 */
public class StudentDaoImpl2 {public static void main(String[] args) {Connection connnull; Statement stmtnull;ResultSet rsnull;try {//使用工具类 获得一个连接对象 conn JdbcUtils2.getConnection();stmt conn.createStatement();String sql select * from student ;rs stmt.executeQuery(sql);while(rs.next()){
// Object object rs.getObject(1);int id rs.getInt(sid);String name rs.getString(sname);System.out.println(id name);}} catch (SQLException e) {e.printStackTrace();} finally{JdbcUtils2.release(rs, stmt, conn);}}
}上面的代码是硬编码如果是软编码的话步骤如下
1个方言文件内容
# this is my mysql configuration
driver com.mysql.jdbc.Driver
url jdbc:mysql://localhost:3306/jdbc01
username root
password root
2 jdbcutil.java读取如下
package com.yidongxueyuan.dao;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;/** 读取外部的配置文件*/
public class JdbcUtil {private static final Properties p new Properties();private static String url; private static String driver; private static String username; private static String password; static{try {//将外部的配置文件进行读取 类的加载器 InputStream in JdbcUtil.class.getClassLoader().getResourceAsStream(dbconfig.properties);p.load(in);driver p.getProperty(driver);Class.forName(driver);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}/** 1创建工具类 直接获得一个连接对象 */public static Connection getConnection (){try {url p.getProperty(url);username p.getProperty(username);password p.getProperty(password);// Connection connection DriverManager.getConnection(url, username, password);return connection; } catch (SQLException e) {throw new RuntimeException(e);}}//释放连接 /** 定义一个方法 释放资源 直接将rs stmt conn 全部释放 */public static void release(ResultSet rs ,Statement stmt , Connection conn){if(rs!null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}rsnull;}if(stmt!null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}stmtnull;}if(conn!null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}connnull;}}/*public static void main(String[] args) {Connection connection getConnection();System.out.println(connection);}*/}使用上面的工具类完成增加改查操作 package com.yidongxueyuan.dao2;import java.sql.Connection;
import java.sql.Statement;
import java.util.Date;import org.junit.Test;import com.yidongxueyuan.dao.JdbcUtil;public class StudentCRUD {//insert 插入 Testpublic void insertStudent() throws Exception {Connection conn JdbcUtil.getConnection();Statement stmt conn.createStatement();int num stmt.executeUpdate(insert into student (sname, birthday) values(xxx,new java.sql.Date(System.currentTimeMillis())));if(num0){System.out.println(插入成功);}else{System.out.println(插入失败);}JdbcUtil.release(null, stmt, conn);}//删除 Testpublic void deleteStudent() throws Exception {Connection conn JdbcUtil.getConnection();Statement stmt conn.createStatement();String sqldelete from student where sid2 ;int num stmt.executeUpdate(sql);System.out.println(num:num);if(num0){System.out.println(删除成功);}else{System.out.println(删除失败);}JdbcUtil.release(null, stmt, conn);}//修改 Testpublic void updateStudent() throws Exception {Connection conn JdbcUtil.getConnection();Statement stmt conn.createStatement();String sqlupdate student set snameyyyyy where sid 3 ;int num stmt.executeUpdate(sql);System.out.println(num:num);if(num0){System.out.println(修改成功);}else{System.out.println(修改失败);}JdbcUtil.release(null, stmt, conn);}//查 }