数据连接:JDBC+MySql

来源:互联网 发布:讲诚信知敬畏发言稿 编辑:程序博客网 时间:2024/05/19 02:00

这几天上手练习了一下Mysql数据库的使用,但是要在JAVA项目中无法直接操纵Mysql数据库,操作Mysql数据库需要一个桥梁,今天练习的就是其中一个重要的桥梁,那就是JDBC(JAVA Data Base Connectivity),即JAVA数据库连接。使用JDBC从安装到使用包括这几个步骤:

     1、已经安装Eclipse、MySql,并且都可以正常使用

     2、下载eclipse的Mysql支持包,下载地址http://www.mysql.com/downloads/connector/j/,下载之后得到后缀名为.zip的文件,解压之后,将其中后缀名为.jar的文件导入到当前包中,具体方法为:选中当前包,右键->构建路径->配置构建路径->点击库栏->添加JAR文件。英文或者其他版本的eclipse添加方法大同小异。

     3、现在开始就可以真正在JAVA项目中使用Mysql了。首先看一下本次练习使用的数据表。


     4、第一步,在操作MySql中的数据之前,首先要获取与数据库的连接,这步操作是以下几个步骤的基础,其具体代码如下:

//连接的获取,在操作之前必须先获取与数据库的连接private static Connection getConn(){String driver = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/first?characterEncoding=utf8&useSSL=true";String username = "root";String password = "root";Connection conn = null;try{Class.forName(driver);conn = (Connection) DriverManager.getConnection(url,username,password);}catch(ClassNotFoundException e){e.printStackTrace();}catch(SQLException e){e.printStackTrace();}return conn;}
需要注意的是,在版本较新的eclipse中,必须在url中加上“?characterEncoding=utf8&useSSL=true”,否则会报错。

      5、插入新的数据,编写静态函数void insert(int id,String name,int age),传入参数最后插入到数据表student的最后面,注意,虽然数据表总共有三列(id int auto_increment primary key,name char(20),age int),但是传入的参数可以是1个,2个或者3个甚至是0个,这样做法结果就是id自增,其他的为空,在实际应用的时候可以使用函数的重载,来实现数据表中不同个数与类型的参数的数据项的插入。

insert(int id,String name,int age)的代码具体如下:

//插入新的数据private static void insert(int id,String name,int age){Connection conn = getConn();//sql语句中,参数id,name,age这个三个参数可以为1个、2个或者3个String sql = "insert into student (id,name,age) values(?,?,?)";PreparedStatement ps;try{ps = conn.prepareStatement(sql);ps.setInt(1, id);ps.setString(2, name);ps.setInt(3, age);ps.executeUpdate();ps.close();conn.close();System.out.println("数据添加成功!");}catch (SQLException e){e.printStackTrace();}}
主函数代码如下:

public static void main(String[] args){insert(6,"Cherry",100);}
函数执行之后数据表如下图所示:



6、修改数据项中的某列,编写静态函数void update(int id,String name)和void update(int id,int age),通过函数的重载实现。具体的代码如下:

//修改数据项中的名字private static void update(int id,String name){Connection conn = getConn();//注意sql语句中的" ' "符号包围不可省略,不然会报错,但是id可包围也可不包围String sql = "update student set name ='" + name +"'where id= "+id;PreparedStatement ps;try{ps = conn.prepareStatement(sql);ps.executeUpdate();System.out.println("数据更改成功!");ps.close();conn.close();}catch(SQLException e){e.printStackTrace();}}//修改数据项中的年龄private static void update(int id,int age){Connection conn = getConn();//注意sql语句中的" ' "符号包围不可省略,不然会报错,但是id可包围也可不包围String sql = "update student set age='"+ age + "'where id ="+id;PreparedStatement ps;try{ps = conn.prepareStatement(sql);ps.executeUpdate();System.out.println("数据更改成功!");ps.close();conn.close();}catch (SQLException e){e.printStackTrace();}}
实现测试与上一步中的实现类似。

7、数据项的删除。编写静态函数void delete(int id),具体代码如下:

 //删除指定数据项private static void delete(int id){Connection conn = getConn();String sql = "delete from student where id ='"+id +"'";PreparedStatement ps;try{ps = conn.prepareStatement(sql);ps.executeUpdate();System.out.println("数据删除成功!");ps.close();conn.close();}catch(SQLException e){e.printStackTrace();}}
8、指定数据项的查询并打印,编写函数void select(int id),具体代码如下:

//查询指定数据项并打印private static void select(int id){Connection conn = getConn();String sql = "select * from student where id ='"+id+"'";PreparedStatement ps;try{ps = conn.prepareStatement(sql);ResultSet result = ps.executeQuery(sql);//注意使用之前一定要先写上.next()函数,最初光标指向第一行之前,所以需要先调用.next()方法,使其指向当前行,否则会报"Before start of result set"错误result.next();System.out.println(result.getInt("id"));System.out.println(result.getString("name"));System.out.println(result.getInt("age"));}catch(SQLException e){e.printStackTrace();}}
9、查询所有数据项并打印,编写函数void selectAll(),具体代码如下:

//查询所有数据项并打印 private static void selectAll(){Connection conn = getConn();Statement st = null;String sql = "select * from student";try{st = conn.createStatement();ResultSet result = st.executeQuery(sql);while(result.next()){System.out.println(result.getInt("id")+"--"+result.getString("name")+"--"+result.getInt("age"));}}catch(SQLException e){e.printStackTrace();}}





0 0
原创粉丝点击