JDBC简介
JDBC全称为:Java Data Base Connectivity (java数据库衔接),能够为多种数据库供应填一致的接见。JDBC是sun开辟的一套数据库接见编程接口,是一种SQL级的API。它是由java言语编写完成,所以具有很好的跨平台特征,运用JDBC编写的数据库应用程序能够在任何支撑java的平台上运转,而没必要在差别的平台上编写差别的应用程序。【视频教程引荐:Java教程】
JDBC编程步骤
(1)加载驱动程序:
下载驱动包 : http://dev.mysql.com/downloads/connector/j/
解压,取得 jar文件。将该文件复制到Java工程目次Java Resources/Libraries/ 下,→ buildpath 。
(2)取得数据库衔接
(3)建立Statement对象:
(4)向数据库发送SQL敕令
(5)处置惩罚数据库的返回效果(ResultSet类)
package com.baidu.emp.jdbcTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.Driver; /** * 开始运用jdbc衔接数据库 * @author Admin * */ public class Test001 { public static void main(String[] args) throws Exception { /** * 加载驱动 */ // 要领一: /* * import java.sql.DriverManager; import com.mysql.jdbc.Driver; */ // Driver driver = new Driver(); // DriverManager.registerDriver(driver); // 要领二:(引荐运用) Class.forName("com.mysql.jdbc.Driver"); /** * 建立链接 */ String url = "jdbc:mysql://localhost:3306/testjdbc"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); // 建立statement对象 Statement statement = connection.createStatement(); /** * 实行SQL,猎取效果集 */ String sql = "select * from test01"; ResultSet result = statement.executeQuery(sql); // 遍历效果集 while (result.next()) { String name = result.getString("name"); int id = result.getInt("id"); System.out.println(name + "\t" + id); } /** * 封闭链接,开释资本 */ result.close(); statement.close(); connection.close(); } }
防备SQL注入改用prepareStatement
package com.boya.emp.jdbcTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * SQL注入,运用prepareStatement对象举行预编译 * @author Admin * */ public class Test002 { public static void main(String[] args) throws Exception { /** * 加载驱动 */ Class.forName("com.mysql.jdbc.Driver"); /** * 建立链接 */ String url = "jdbc:mysql://localhost:3306/testjdbc"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); // 写SQL String sql = "select * from test01 where id = ?"; //建立statement对象,预编译 PreparedStatement statement = connection.prepareStatement(sql); //设置参数 statement.setInt(1, 2); /** * 实行SQL,猎取效果集 */ ResultSet result = statement.executeQuery(); // 遍历效果集 while (result.next()) { String name = result.getString("name"); int id = result.getInt("id"); System.out.println(name + "\t" + id); } /** * 封闭链接,开释资本 */ result.close(); statement.close(); connection.close(); } }
举行代码优化,设置设置文件,东西类,完成增删该查
增添设置文件轻易修正数据库,用户登录。。。
jdbc.properties(设置文件名)
driverName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/testjdbc userName=root password=root
注重写设置文件时中心不能够有空格,引号之类的
东西类:增强了代码的复用性
package com.baidu.emp.utils; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import org.junit.Test; public class JdbcUtils { static String driverClassName; static String url; static String user; static String password; static { // 建立设置文件对象 Properties properties = new Properties(); // 加载设置文件输入流 InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); // 从新加载设置文件 try { properties.load(inputStream); // 猎取设置文件的值 driverClassName = properties.getProperty("driverName"); url = properties.getProperty("url"); user = properties.getProperty("userName"); password = properties.getProperty("password"); Class.forName(driverClassName); } catch (Exception e) { // 抛出非常 throw new RuntimeException(e); } } /** * 猎取衔接 */ @Test public void testName() throws Exception { System.out.println(driverClassName); } public static Connection getConnection() { Connection connection = null; try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // 抛出非常 throw new RuntimeException(e); } return connection; } /** * 封闭链接,开释资本 */ public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } resultSet = null; // 垃圾实时消灭 //注重,不要弄成死循环 close(connection, statement); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 增编削开释资本 */ public static void close(Connection connection, PreparedStatement statement) { try { if (connection != null) { connection.close(); } connection = null; if (statement != null) { statement.close(); } statement = null; } catch (SQLException e) { throw new RuntimeException(e); } } }
测试增编削查:
package com.baidu.emp.jdbcTest; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.baidu.emp.utils.JdbcUtils; /** * 运用jdbcUtils衔接数据库举行增编削查 * * @author Admin * */ public class Test003 { // 初始化值 Connection connection = null; PreparedStatement statement = null; ResultSet result = null; @Before public void start() throws Exception { // 建立链接 connection = JdbcUtils.getConnection(); System.out.println("建立链接"); } @After public void end() throws Exception { // 封闭链接 JdbcUtils.close(connection, statement, result); System.out.println("封闭链接"); } /** *插进去数据 * @throws Exception */ @Test public void add() throws Exception { String sql = "insert into test01 values(null,?)"; statement = connection.prepareStatement(sql); statement.setString(1, "李四"); int result = statement.executeUpdate(); if (result!=0) { System.out.println("增加胜利"); } } /** * 删除数据 * @throws Exception */ @Test public void del() throws Exception { String sql = "delete from test01 where id =?"; statement = connection.prepareStatement(sql); statement.setInt(1,3); int result = statement.executeUpdate(); if (result!=0) { System.out.println("删除胜利"); } } /** * 修正数据 * @throws Exception */ @Test public void change() throws Exception { String sql = "update test01 set name = ? where id = ?"; statement = connection.prepareStatement(sql); statement.setString(1, "张飞"); statement.setInt(2, 2); int result = statement.executeUpdate(); if (result!=0) { System.out.println("修正胜利"); } } /** * 查询悉数数据 * @throws Exception */ @Test public void findAll() throws Exception { String sql = "select id , name from test01"; statement = connection.prepareStatement(sql); result = statement.executeQuery(); if (result.next()) { System.out.println("查询胜利"); } } /** * 前提查询数据 * @throws Exception */ @Test public void findOne() throws Exception { String sql = "select id , name from test01 where id = ?"; statement = connection.prepareStatement(sql); statement.setInt(1, 2); result = statement.executeQuery(); if (result.next()) { System.out.println("查询胜利"); } } }
存在毛病望列位同仁指出,非常感谢
以上就是本篇文章的悉数内容,愿望能对人人的进修有所协助。更多精彩内容人人能够关注ki4网相干教程栏目!!!
以上就是jdbc怎样衔接数据库?(附代码)的细致内容,更多请关注ki4网别的相干文章!