zoukankan      html  css  js  c++  java
  • JDBC与数据库的连接以及基本的操作

    package wf;
    
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.Statement;
    
    public class driverManager {
        public static void main(String[] args) {
            Connection connection=null;
            Statement statement=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                 connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
    //       String sql="insert into customers values(111,'王锋','1553728655','1994-9-26')";
    //            String sql="delete from customers where id=123";
    //            String sql="update customers set name='小小'"+"where id=123 " ;
                 
                 String sql="select id,name,birth,email from customers";
                 
                 
                 statement=(Statement) connection.createStatement();
                ResultSet rs=statement.executeQuery(sql);
                while(rs.next()){
                    int id=rs.getInt(1);//下标从1开始
                    String name=rs.getString(2);
                    String email=rs.getString(3);
                    Date birth=rs.getDate(4);
                    System.out.println(id);
                    System.out.println(name);
                    System.out.println(email);
                    System.out.println(birth);
                    
                }
               statement.execute(sql);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(statement!=null){
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally{
                        if(connection!=null){
                            try {
                                connection.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                    
                }
                }
        }
    }
  • 相关阅读:
    SQL
    第九章
    第三章 表单
    第二章 表格,列表,媒体元素
    HTML5基础
    Java第一本书总复习
    字符串
    人机猜拳
    类的无参方法
    类和对象
  • 原文地址:https://www.cnblogs.com/alhh/p/5535279.html
Copyright © 2011-2022 走看看