Eclipse下Java链接MySQL数据库
来源:互联网 发布:淘宝全自动刷单软件 编辑:程序博客网 时间:2024/05/16 17:57
一、下载JDBC
二、打开Eclipse,左键选中Java项目,右键Bulid Path–>Add External Archives–>选择你下载好的JDBC
三、创建连接类Connectors
public class Connectors { public static Connection conn = null; //创建连接 public static Connection getConn(){ //数据库名="jdbc:mysql://[ip地址]:[端口号]/[数据库名]" String url = "jdbc:mysql://localhost/novel"; //驱动程序名 String name = "com.mysql.jdbc.Driver"; //用户名 String user = "root"; //密码 String password = "root1234"; try{ //注册驱动 Class.forName(name); //建立连接 conn = DriverManager.getConnection(url, user, password); }catch (ClassNotFoundException e) { e.printStackTrace(); }catch ( SQLException e){ e.printStackTrace(); } //返回连接 return conn; } //关闭连接 public static void closeConn() { if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } }}
四、数据库操作类DataDAO
public class DtataDAO{ /** 获取数据库中所有加密记录 * @param vecRecordBean 引用参数,存储获得的加密记录 */ public static void selectData(Vector<RecordBean> vecRecordBean){ //数据库连接 Connection conn = null; //创建PreparedStatement 对象 PreparedStatement psmt = null; //sql语句 String querySql = "select * from table"; //结果集 ResultSet rs = null; try{ //创建连接 conn = Connectors.getConn(); //执行sql语句 psmt = conn.prepareStatement(querySql); rs = psmt.executeQuery(); while(rs.next()){ RecordBean tempBean = new RecordBean(); tempBean.setRecordId(rs.getInt(1)); tempBean.setRecordContent(rs.getString(2)); //依次获取数据库中每条列记录 vecRecordBean.addElement(tempBean); } psmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ //关闭连接 Connectors.closeConn(); } } /** 批量将源数据插入到数据库中 * @param vecRecordBean源数据记录 */ public statuc void insertData(Vector<RecordBean> vecRecordBean){ Connection conn = null; PreparedStatement psmt = null; //批量插入的上限 int countInsert = 0; String insertSql = "insert into table value(?, ?)"; try{ conn = Connectors.getConn(); //不自动提交事务 conn.setAutoCommit(false); psmt = conn.prepareStatement(insertSql); //批量插入数据 for(int i = 0; i < vecRecordBean.size(); i++){ psmt.setInt(1, vecRecordBean.get(i).getRecordId()); psmt.setString(2, vecRecordBean.get(i).getRecordContent() ); psmt.addBatch(); //每一千条插入数据库 if(++countInsert % 1000 == 0){ psmt.executeBatch(); //提交事务 conn.commit(); } } psmt.executeBatch(); //剩下的插入数据库 conn.commit(); //提交事务 psmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ Connectors.closeConn(); } } /** 更新数据库中记录 */ public static void updateData(){ Connection conn = null; PreParedStatement psmt = null; String newContent = "xxxxx"; String updatSql = "update table set record_content = ? where record_id = 1"; try{ conn = Connectors.getConn(); psmt.setString(2, newContent); psmt.executeUpdate(); psmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ Connectors.closeConn(); } } /** 删除数据库中记录 */ public static void deleteData( ){ Connection conn = null; PreParedStatement psmt = null; String delteSql = "delete from table"; try { conn = Connectors.getConn(); psmt = conn.prepareStatement(deletSql); psmt.executeUpdate(); psmt.close(); } catch(SQLException e){ e.printStackTrace(); }finally{ Connectors.closeConn(); } }}
备注:常见的增、删、改、查以及like查询sql语句书写
1、insert语句
格式:insert into 表名(列名1, 列名2,…) value(?, ?, ….)
例:
String insertSql1 = “insert into table(record_id , record_content) value(1, ‘const string’)”;
String insertSql2 = “insert into table(record_id , record_content) value(?, ?)”;
当插入的是表的全部列属性时,可以省去列名项
2、delete语句
格式:delete from 表名 where 列名 = ’ “+String变量+” ’
例:
String dalateSql1 = “delete from table where record_id = 1”;
String deleteSql2 = “delete from table where record_id = ’ “+string+” ‘;
3、update语句
格式:update 表名 set 列名 = ?,列名2 = ? where 列名i = ?;
例:
String updateSql1 =” update table set record_content = ’ xxx’ where record_id = 1 “;
String updateSql2 =” update table set record_content = ’ “+content” ’ where record_id = “+id+” “;
4、select语句
格式:select 列名1,列名2, … from 表名 where 列名 = ?;
例:
String selectSql1 = ” select * from table where record_id = 1 “;
String selectSql2 = ” select * from table where record_content = ’ “+string+” ’ “;
5,like语句
格式:select from 表名 where 列名 like ’ s%/%s/%s%’
例:
String likeSql1= “select * from table where record_content like ‘%xxx’ “;
String likeSql2= “select * from table where record_content like ‘% “+string+” ’ “;
- Eclipse下Java链接MySQL数据库
- Eclipse链接MySQL数据库
- Eclipse链接MySQL数据库
- ECLIPSE 链接数据库 MYSQL
- eclipse 链接 mysql数据库
- ubuntu16.04下MySQL驱动、使用eclipse链接数据库
- eclipse链接数据库mysql登陆
- java链接mysql数据库
- JAVA链接Mysql数据库
- java链接mysql数据库
- java链接mysql数据库
- java链接MySql数据库
- java链接mysql数据库
- java链接mysql数据库
- linux下链接mysql数据库
- java 链接mysql 链接数据库的步骤
- Java链接MySQL数据库 操作
- Java之MySql数据库链接
- HTTP协议中的长连接与短连接
- assert是一个预处理宏
- Java核心技术——4对象与类——1
- 黑马程序员-----二、Android开发日常积累之-----安卓网络图片加载
- swipemenulistview
- Eclipse下Java链接MySQL数据库
- ERP实施项目的计划阶段要点分析
- C/C++中相对路径与绝对路径以及斜杠与反斜杠的区别
- 关于Java交换两个对象的问题
- 修改ZigBee协调器内存大小
- php、asp 发起post请求
- toj 3072 Train Order
- OC 学习第六天_Block的使用
- 接口和抽象类的区别