JDBC connection operation data to the database

   What is JDBC? JDBC, what role do? For starters usually produce these curious, here I am with you to find out.
  First, JDBC bridge JAVA and database programmer to provide read and write operations. JDBC used to execute SQL statements JAVA API, you can provide unified access to multiple relational databases.
  You can connect Oracle, MySql, SqlServer following databases through JDBC. JDBC Data circulation between the application server and the database DB, to achieve operation of the database server application.
  FIG JDBC implementation process is as follows:
Here Insert Picture Description
  The following tables give the user Liezi, queries the user data table. First, prepare a user entity class table. Generally create a User entity class in vo bag.

    1 package com.gx.vo;
    2
    3 public class User{
    4     private String username;
    5     private String  password;
    6     private int userID;
    7     
    8     public String getUsername(){
    9        return username;
    10     }
    11    public void setUsername(String username){
    12        this.username=username;
    13      }
    14    public String getPassword(){
    15        return password;
    16      }
    17    public void setPassword(String password){
    18        this.password=password;
    19      }
    20    public int getUserid(){
    21        return userid;
    22    }
    23     public void setUserid(int userid){
    24        this.userid=userid;
    25    }
    26 }

  Entity class is ready, you also need some configuration database. Now to connect MySql example. Click the "right arrow" in the upper right corner of MyEclipse 10, select "MyEclipse Database Explorer" appears "DB Brow" in the left navigation MyEclipse 10 Then, right-click in the blank, then "New" to create a new Database Driver.

  The connection is successful as shown below:
Here Insert Picture Description
Note:
  1, select the type of database you want to connect, the value of Driver name can easily play
  2, URL particular attention to their respective URL are not the same, depending on the connection to the database,
      the Oracle's URL: jdbc: oracle : Thin: @localhost: 1521: orcl
      the URL of the MySql: jdbc: MySQL: // localhost: 3306 / the Test
      the URL of the SqlServer: jdbc.microsoft:sqlserver://localhost:1433;DatabaseName=test
      the Oracle service port 1521; MySql the service port 3306; SqlServer service port 1433
  3, the User name and Password for the corresponding databases and
  4, add the mysql-connector-java-5.1.22- bin jar package
  5, click on "test Driver" test the database connection is successful If the prompt contains the instructions that you successfully connect to the database successfully.
  The most important thing is to sql-connector-java-5.1.22- bin jar package, copy the lib under the WEB-INF under WebRoot in our project.
  The basic preparatory work has been completed, then the next step is to write JDBC code in util (tools) package.
  To complete JDBC interact with the database, you need five steps.

    1 package com.gx.util;
    2
    2 import java.sql.Connection;
    3 import java.sql.DriverManager;
    4 import java.util.ArrayList;
    5 import java.util.List;
    6
    7 import com.gx.vo.User;
    8  public class Jdbcutil{
    9     private static String username=”root”;
    10    private static String password=”root”;
    11    private static String url=”jdbc:mysql://localhost:3306/test”;
    12    private static String driver=”com.mysql.jdbc.Driver”;
    13    
    14    //driver的URL要与数据库的配置中的Driver classname的对应
    15    
    16    public static void selectAll(){
    17      try{
    18          User user=null;
    19          List<User> users=null;
    20          users=new ArrayList<User>();
    21          //第一步:加载驱动
    22          Class.forName(driver);
    23          //第二步:通过DriverManager获取数据库的连接Connection
    24         Conection con=DriverManager.getConnection(url,username,password);
    25         //第三步:从数据库连接Connection创建Statement或者*PreparedStatement
    26         Statement st=con.createStatement();
    27         //第四步:使用PreparedStatement或者Statement执行sql并返回结果集
    28         ResultSet rs=st.executeQuery(“select *from user”);
    29         //新增、删除、修改都是用executeUpdate; 查询用的是exeuteQuery
    30         //遍历所有的数据
    31         while(rs.next()){
    32             user=new User();
    33             user.setUserid(rs.getInt(“userid”));
    34             user.setUsername(rs.getString(“username”));
    35             user.setPassword(rs.getString(“password”));
    36             usres.add(user);
    37            }
    38         //读取集合里面的数据
    39          for(User user2 :users){
    40               System.out.println(“id”+user2.getUserid()+ “名称”+user2.getUsername()+”密码”+
    41               user2.getpassword());
    42          //第五步:关闭rs、st、con、;要特别的注意关闭的顺序要倒过来
    43              rs.close();
    44              st.close();
    45              con.close();
    46           }cath(ClassNotFoundException e){
    47              e.printStackTrace();
    48           }cath(SQLException e){
    49              e.printStackTrace();
    50           }
    51           }

The results as shown below:
Here Insert Picture Description
  Over here! I do not eleven to add, modify, delete and then repeat once again, and in fact, these queries are very similar, it's steps are the same. Required when adding and deleting User vo package implementation class; there needs to be noted that in the course of implementation add, delete, modify are using executeUpdate; queries are exeuteQuery. I have to say that we may be wrong that may occur when connecting to the database.
    1, if the process is carried out newspaper "java.lang.ClassNotFoundException: con.jdbc.mysql.Driver"
the wrong time.
Solution: First, you have not added to see mysql-connector-java-5.1.22- bin jar package; second, check your configuration database and your statement corresponds.
    2, if in the course of the implementation of the new report "com.mysql.jdbc.MysqlDataTruncation: Data too long for colum 'at' row 1" the wrong time.
Solution: The new data show that you exceed the length of the data inventory value, so you can modify the database or modify the new content. Depending on individual needs may be
   3, if in the course of the implementation of the newspaper "com.mysql.jdbc.exceptions.jdbc.MySQLSyntaxErrorException" the wrong time.
Solution: syntax error description on what you write sql statement, see the sql statement is correct.
  Here we have a certain understanding of JDBC. I talk about why I write JDBC connection operation data to the database, although this is very basic stuff, but I think is very important. In doing some small projects or programs will inevitably deal with the database, so too! How to connect How to connect the database will not, say what are empty. Although the foundation is very simple, but we must firmly lay the foundation. Remember JDBC connection to the database, easily won just five steps.

Published 37 original articles · won praise 8 · views 5958

Guess you like

Origin blog.csdn.net/weixin_43741599/article/details/90273291