您的位置:澳门新葡8455最新网站 > 编程教学 > jdbc连接数据库,JDBC学习笔记

jdbc连接数据库,JDBC学习笔记

发布时间:2019-10-07 12:56编辑:编程教学浏览(79)

    1.Staement执行DDL语句

    package com.huan.statemennt;import com.mysql.jdbc.Driver;import org.junit.Test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;/** * Created by pc on 17-5-1. */public class StatementDemo { String user = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/eurasia_echarts"; Connection conn = null; Statement state = null; @Test public void test1() throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); if (null != conn) { System.out.println("数据库连接成功"); } //创建statement对象 state = conn.createStatement(); //创建sql语句 String sql = " create table mahuan(id int primary key auto_increment ,name varchar,age int); "; //发送sql语句,执行并返回结果 int count = state.executeUpdate; System.out.println; } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 if (null != state) { state.close(); } if (null != conn) { conn.close(); } } }}
    

    图片 1图片 2

    使用JDBC连接数据库,jdbc连接数据库

    JDBC(Java Data Base Connectivity)数据库连接,我们在编写web应用或java应用程序要连接数据库时就要使用JDBC。使用JDBC连接数据库一般步骤有:

    1、加载驱动程序 Class.forName(driver);

    2、创建连接对象 Connection con = DriverManager.getConnection(url,username,password);

    3、创建sql语句执行对象 

    4、执行sql语句

    5、对执行结果进行处理

    6、关闭相关的连接对象(顺序跟声明的顺序相反)

    下面是以建立与MySQL数据库连接的例子,其他数据库的过程类似

     

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class DBConnection
    {

    public static void main(String[] args)
    {
    String driver = "com.mysql.jdbc.Driver";

    //localhost指本机,也可以用本地ip地址代替,3306为MySQL数据库的默认端口号,“user”为要连接的数据库名
    String url = "jdbc:mysql://localhost:3306/user";

    //填入数据库的用户名跟密码
    String username = "test";
    String password = "test";
    String sql = "select * from user";//编写要执行的sql语句,此处为从user表中查询所有用户的信息

    try
    {
    Class.forName(driver);//加载驱动程序,此处运用隐式注册驱动程序的方法

    }
    catch(ClassNotFoundException e)
    {
    e.printStackTrace();
    }
    try
    {
    Connection con = DriverManager.getConnection(url,username,password);//创建连接对象
    Statement st = con.createStatement();//创建sql执行对象
    ResultSet rs = st.executeQuery(sql);//执行sql语句并返回结果集

    while(rs.next())//对结果集进行遍历输出
    {

    System.out.println("username: "+rs.getString(1));//通过列的标号来获得数据
    System.out.println("useradd: "+rs.getString("useradd"));//通过列名来获得数据
    System.out.println("userage: "+rs.getInt("userage"));
    }
    //关闭相关的对象
    if(rs != null)
    {
    try
    {
    rs.close();
    }
    catch(SQLException e)
    {
    e.printStackTrace();
    }
    }

    if(st != null)
    {
    try
    {
    st.close();
    }
    catch(SQLException e)
    {

    e.printStackTrace();
    }
    }

    if(con !=null)
    {

    try
    {

    con.close();
    }
    catch(SQLException e)
    {
    e.printStackTrace();
    }
    }
    }
    catch(SQLException e)
    {
    e.printStackTrace();
    }

    }

    }

     

    2.staement执行执行DML语句

          • Dao.java
    package com.huan.dao;import org.junit.Test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;/** * Created by pc on 17-5-1. */public class Dao { static String user ="root"; static String password = "root"; static String url = "jdbc:mysql://localhost:3306/eurasia_echarts?characterEncoding=UTF-8"; @Test public static Connection getDao() throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, user, password); if (null != conn) { System.out.println; } return conn; } public static void close(Statement state, Connection conn){ if(null != state){ try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(null != conn){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }}
    

    Dml_Demo.java

    package com.huan.statemennt;import com.huan.dao.Dao;import org.junit.Test;import java.sql.Connection;import java.sql.Statement;/** * Created by pc on 17-5-1. */public class Dml_Demo { String user ="root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/eurasia_echarts?characterEncoding=UTF-8"; Connection conn = null; Statement state = null; @Test public void test() throws Exception{ conn =Dao.getDao(); state = conn.createStatement(); //插入数据 String sql = " insert into mahuan  values; "; //更新数据 //String sql = " update mahuan set name ='马欢' where id =3; "; //删除数据 // String sql = " delete from mahuan where id = 3; "; int count = state.executeUpdate; System.out.println; Dao.close(state,conn); }}
    

    图片 3图片 4

    java中使用JDBC连接数据库的步骤?

    1.注册驱动
    Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动
    2.获取数据库连接
    java.sql.Connection conn=java.sql.DriverManager.getConnection(); 3.获取表达式
    java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三个参数分别是数据库连接的URL,用户名,密码 4.执行SQL
    java.sql.ResultSet rs=stmt.executeQuery("select * from user"); 5.显示结果集里面的数据
    while(rs.next()){
    System.out.println(rs.getInt(1));
    System.out.println(rs.getString("username"));
    System.out.println(rs.getString("password"));
    System.out.pringln();
    }//执行插入语句
    //stmt.executeUpdate("insert into user values(1,'中文','345')");
    6.释放资源
    rs.close();
    stmt.close();
    conn.close();  

    3.staement执行DQL语句

    图片 5

    • ##### 查询数据

      package com.huan.statemennt;import com.huan.dao.Dao;import org.junit.Test;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;/* Created by pc on 17-5-1. /public class Dql_demo { @Test public void test() throws Exception { Connection conn = null; Statement stmt = null; conn = Dao.getDao(); stmt = conn.createStatement(); String sql = " select from mahuan; "; //接收查询到的所有成绩 ResultSet rs =stmt.executeQuery; while){ System.out.println("用索引取值:编号:"+rs.getInt +" 姓名:"+rs.getString+" 年龄:"+rs.getString; System.out.println("用列名称取值:编号:"+rs.getInt +" 姓名:"+rs.getString+" 年龄:"+rs.getString; } Dao.close(stmt,conn); }}

    图片 6

    java中使用JDBC连接数据库的步骤?

    1.注册驱动
    Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动
    2.获取数据库连接
    java.sql.Connection conn=java.sql.DriverManager.getConnection(); 3.获取表达式
    java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三个参数分别是数据库连接的URL,用户名,密码 4.执行SQL
    java.sql.ResultSet rs=stmt.executeQuery("select * from user"); 5.显示结果集里面的数据
    while(rs.next()){
    System.out.println(rs.getInt(1));
    System.out.println(rs.getString("username"));
    System.out.println(rs.getString("password"));
    System.out.pringln();
    }//执行插入语句
    //stmt.executeUpdate("insert into user values(1,'中文','345')");
    6.释放资源
    rs.close();
    stmt.close();
    conn.close();  

    JDBC(Java Data Base Connectivity)数据库连接,我们在编写web应用或java应用程序要连接数据库时就要使用JDBC。...

    文章文集:JavaEE--学习笔记

    本文由澳门新葡8455最新网站发布于编程教学,转载请注明出处:jdbc连接数据库,JDBC学习笔记

    关键词:

上一篇:EL表达式与jsp标签

下一篇:没有了