摘要:基本连接加载驱动建立连接其中这里的是自己创建的数据库的名字是数据库的管理员,是密码下面直接连接数据库,返回的是接口对象驱动名称管理员和密码都是加载驱动,但是会有异常,因此要避免异常获得数据库连接返回如果出现异常就会返回查询数据首先根据所得的
基本连接
</>复制代码
加载驱动: 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,具体见官方文档
欢迎浏览本人博客
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/67030.html
摘要:是访问数据库的标准规范。提供了一种基准据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。代码如下工具类数据库驱动注册失败提供获取连接的方法获得连接返回连接 本文为大家介绍 Java 如何使用JDBC 连接 MySQL 数据库。 JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java A...
摘要:是访问数据库的标准规范。提供了一种基准据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。代码如下工具类数据库驱动注册失败提供获取连接的方法获得连接返回连接 本文为大家介绍 Java 如何使用JDBC 连接 MySQL 数据库。 JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java A...
阅读 2081·2021-11-22 15:33
阅读 3083·2021-11-18 10:02
阅读 2724·2021-11-08 13:16
阅读 1725·2021-10-09 09:57
阅读 1463·2021-09-30 09:47
阅读 2087·2019-08-29 13:05
阅读 3144·2019-08-29 12:46
阅读 1088·2019-08-29 12:19