java 建立连接mysql_java连接mysql

基本连接

加载驱动: 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,具体见官方文档

欢迎浏览本人博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值