MyEclipse与Mysql数据库的连接
来源:互联网 发布:大秦铁路知乎 编辑:程序博客网 时间:2024/05/01 08:57
1.加载MySql驱动程序
Class.forName("com.mysql.jdbc.Driver"); // 加载MySql驱动程序
2.建立Connection连接对象
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?user=root&password=root");
其中
url = "jdbc:mysql://localhost/";
serverName = "localhost";
portNumber = "3306";
databaseName = "my_db";
userName = "root";
password = "root";
3.建立Statement语句对象
Statement st = con.createment();
4.执行sql语句
st.executeUpdate(“SQL语句”);//插入
st.executeUpdate(“SQL语句”);//更新
st.executeexecuteQuery(“SQL语句”);//查询
5.关闭连接
con.close();
6.实现案例
DBConn.java中是对连接数据库的实现
package db;import java.sql.Connection;import java.sql.DriverManager;public class DBConn {/* 获取Conncetion对象并返回 */public Connection getConnection() {Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); // 加载MySql驱动程序 //con = DriverManager.getConnection("jdbc:mysql://localhost/cloud_db?user=root&password=root&autoReconnect=true&useUnicode=true&characterEncoding=utf8"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?user=root&password=root&useUnicode=true&characterEncoding=GBK"); //jdbc:mysql://localhost/数据库名称?useUnicode=true&characterEncoding=GBK } catch (Exception e) { System.out.println("数据库获取失败!"); e.printStackTrace(); } return con; }}
StudentImpl.java是对数据库数据的录入
package impl;import pojo.Student;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import db.DBConn;public class StudentImpl {public String joinString(String[] slikes) {String joinlikes = "";for (String temp : slikes) {joinlikes += temp + "-";}return joinlikes.substring(0, joinlikes.length() - 1);}public void saveStudent(Student student){//连接数据库完成数据的录入操作Connection con = null;PreparedStatement pre = null;DBConn conns = new DBConn();con = conns.getConnection();try{pre = con.prepareStatement("insert into student (sname,spass,Slikes) values(?,?,?)");pre.setString(1,student.getSname());pre.setString(2,student.getSpass());pre.setString(3,student.getSlikes());boolean b = pre.execute();}catch(SQLException e){e.printStackTrace();}finally{try {con.close();} catch (SQLException e) {e.printStackTrace();}}}}
0 0
- MyEclipse与Mysql数据库的连接
- Myeclipse与mysql数据库的连接
- MySQL与MyEclipse的连接
- MyEclipse连接数据库MySQL
- Myeclipse连接MySQL数据库
- MyEclipse 连接MySQL数据库
- Myeclipse连接mysql数据库
- myeclipse连接mysql数据库
- Myeclipse连接Mysql数据库
- myeclipse连接mysql数据库
- MyEclipse连接MySQL数据库
- Myeclipse连接mysql数据库
- Myeclipse连接 数据库MySql
- myeclipse连接mysql数据库
- MyEclipse连接MySQL数据库
- MyEclipse连接远程的MYSQL数据库
- mysql与myeclipse连接
- mysql的安装与简单使用以及myeclipse下与数据库的连接和使用
- 小姐,请你躺文明些
- C++中引用成员的初始化
- std::vector push顺序
- 周鸿祎:大数据时代的用户信息安全
- Android学习----异常(2):Please ensure that adb is correctly located at ' ... '
- MyEclipse与Mysql数据库的连接
- SQL语句总结
- Linux/UNIX之标准IO库
- wlan和wfi的区别
- js + php 读取、播放视频流 兼容firefox,chrome,ie,mac的safari,ios的safari,ios的微信浏览器(curl读取文件流篇)
- ZooKeeper-3.4.6分布式安装指南
- 第九周项目2-Time类运算符重载
- 游戏架构 游戏架构设计(2)
- C语言位域定义及使用