java连接mysql
来源:互联网 发布:linux的rpm -c 编辑:程序博客网 时间:2024/06/03 20:44
基本连接
加载驱动:
Class.forName(com.mysql.jdbc.Driver)
建立连接:
Connection conn=DriverManager.getConnection(url,user,password)
其中
url="jdbc:mysql://localhost:3306/java_demo"
,这里的java_demo
是自己创建的数据库的名字,user
是mysql
数据库的管理员,password
是密码
下面直接连接数据库,返回的是接口Connection对象
import java.sql.*;public static Connection getConnection(){ Connection conn; String driver="com.mysql.jdbc.Driver"; //驱动名称 String url="jdbc:mysql://localhost:3306/java_demo"; //url String user="root"; String password="root"; //管理员和密码都是root try{ Class.forName(driver); //加载驱动,但是会有ClassNotFoundException异常,因此要避免异常 try{ conn = Dri verManager.getConnection(url, user, password); //获得数据库连接 return conn; //返回conn }catch(SQLException e) { e.printStackTrace(); } }catch (ClassNotFoundException e) { e.printStackTrace(); } return null; //如果出现异常就会返回null}
查询数据
**首先根据所得的
Connection
对象创建Statement
对象:Statement statement = connection.createStatement()
;写查询语句:
String sql="select * from student;"
这里是查询所有student中的数据,详细内容请看我的SQL干货篇二创建ResultSet对象存储查询结果:
ResultSet res=statement.executeQuery(sql)
,详细的内容请看官方文档ResultSet详细用法
代码
String sql="select * from student"; if(!conn.isClosed()) { Statement statement=conn.createStatement(); //这里的conn是上面连接数据库的返回的Connection对象 ResultSet res=statement.executeQuery(sql); //执行查询,注意这里只能是executeQuery,Statement还有一些执行mysql函数,但是都不适合查询,后面会详细说 while(res.next()) //如果res结果中还有元素,那么返回true,否则返回的是false,用来判断是否res中还有结果 { int id=res.getInt("id"); //得到id,这里的id是student表中的属性名 对应的时int BigInt smallint..... String name=res.getString("name"); //得到姓名,对应的是mysql中的Char varChar类型 } }
当然上面只是对于基本的查询数据,在一些项目中根本用不到,因为不太灵活,上面的方法只适合全局查询,并不适合在项目中根据条件查询,下面介绍预编译sql语句的接口PrepareStatement
首先编写sql语句:
sql="select * from student where id=?;";
,这里的?
表示一个占位,将条件在后面给出,但是这里一定要用?
创建对象:
PrepareStatement pre=conn.preparestatement(sql);
这里传入参数sql
设置
sql
中的条件语句,填补占位?
的值:pre.setInt(1,1);这里的SetInt
设置id
值的为1,因为这的id
是int
类型的,第一个参数是表示prepareindex
,就是表示第一个占位?
,当然第二个就是2,其中还有SetString(prepareindex String var)
,用来给定表中的char
后者varchar
类型的值代码:
if(!connection.isClosed()) { String sql="select * from course where id=?,name=?"; PreparedStatement preparedStatement=connection.prepareStatement(sql); preparedStatement.setInt(1,1); //给定条件中的值 prepareStatement.setString(2,"jack"); //为第二个?赋值 ResultSet res=preparedStatement.executeQuery(); //执行查询,返回的仍然是ResultSet对象 while(res.next()) { int id=res.getInt("id"); String name=res.getString("name"); System.out.println(id+"--"+name); } }
插入数据
插入数据和上面的两种方法基本是一样的,不同的是
mysql
语句不同,还有的就是执行语句改成了executeUpdate(sql)
,下面的代码值给出了预编译对象的方法,另外一种的方法使用范围并不是很大,只要把上面的查询改为executeUpdate
即可代码:
public static int save(MemoBean memo) { String sql = "insert into student (username, title, content, momotype, memotime) values (?, ?, ?, ?, ?);"; Connection conn = getConnection(); PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setString(1, memo.getUsername()); //设值value中的值 ps.setString(2, memo.getTitle()); ps.setString(3, memo.getContent()); ps.setString(4, memo.getMemotype()); ps.setString(5, memo.getMemotime()); return ps.executeUpdate(); //这里使用的是excuteUpdate } catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); //关闭预编译对象 } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); //关闭Connection对象 } catch (SQLException e) { e.printStackTrace(); } } } return -1; //没有插入成功返回-1 }
更新数据
这里是同样的思路,和插入的基本是一样,只需要改变sql语句即可
代码:
public static int update(MemoBean memo) { String sql = "update student set username=?,title=?,content=?,momotype=?,memotime=? where id=?;";//查询语句 Connection connection = getConnection(); PreparedStatement ps = null; try { ps = connection.prepareStatement(sql); ps.setString(1, memo.getUsername()); //设置条件语句中的值 ps.setString(2, memo.getTitle()); ps.setString(3, memo.getContent()); ps.setString(4, memo.getMemotype()); ps.setString(5, memo.getMemotime()); ps.setInt(6,memo.getId()); return ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { if(ps!=null) { try { ps.close(); }catch (SQLException e) { e.printStackTrace(); } } if(connection!=null) { try { connection.close(); }catch (SQLException e) { e.printStackTrace(); } } } return -1; }
最后说
上面的代码是从自己项目中截取的一部分代码,这个是比较适用于面向对象的,也是最常用的对于目前来看
上面只是给出了查询,插入,更新,因为这是最常用到的方法,其中还有创建表,删除表,当然还有一些他的,这里的创建表直接用
execute(sql)
即可执行,删除表也是用execute(sql)
即可执行,当然如果要按照指定的条件删除,那么可以使用预编译对象执行其中
executeUpdate(sql)
适用于create
,insert
,update
,delete
,但是executeQuery(sql)
适用于select
,具体见官方文档
欢迎浏览本人博客
- 【Java】Java连接MySql
- java连接MySql数据库!
- JAVA 连接 MySQL 数据库
- java连接mysql
- java,jsp连接mysql
- java连接mysql
- java连接mysql数据库
- java连接mysql数据库
- JAVA连接MYSQL配置
- java连接mysql数据库
- java连接mysql
- Java连接MySQL数据库
- JAVA 连接MySQL
- java连接mysql
- Java连接mysql数据库
- 利用java连接mysql
- java连接MySQL数据库
- java连接mysql数据库
- C#读取Excel的简单方法
- 字符串的逆序递归实现 C语言实现
- django Rest Framework 系列 4
- Android 开发的新控件
- 蓝桥杯-基础试题-试笔-字母图形
- java连接mysql
- 杭电acm:4994 Revenge of Nim
- leetcode 98. Validate Binary Search Tree
- 微信测试号申请流程
- 方程式ETERNALBLUE:Windows SMB远程溢出漏洞复现笔记
- 第十六讲项目5.1
- Erlang基本语法汇总【转】
- this全面解析
- 【Java语言】Ja.3.1--Java小应用之二维码