美文网首页
2018-01-11

2018-01-11

作者: 毛毛特工 | 来源:发表于2018-01-11 06:31 被阅读0次

    package com.tis.genshinhikaku;

    import java.sql.*;

    public class readDabase {

        String jdbcClassName="com.ibm.db2.jcc.DB2Driver";

        String url="jdbc:db2://localhost:50000/SAMPLE:currentSchema=DB2ADMIN;";

        String user="db2admin";

        String password="admin";

        ResultSet rs;

    public Connection connectDB () {

    Connection connection = null;

            try {

                //Load class into memory

                Class.forName(jdbcClassName);

                //Establish connection

                connection = DriverManager.getConnection(url, user, password);

                System.out.println("Connecting!");

            } catch (ClassNotFoundException e) {

                e.printStackTrace();

            } catch (SQLException e) {

                e.printStackTrace();

            }

            return connection;

        }

    public void closeDbConnect(Connection conn) {

    if (conn != null) {

    System.out.println("Connected successfully.");

                try {

                conn.close();

                    System.out.println("Connected successfully.");

                } catch (SQLException e) {

                    e.printStackTrace();

                }

    }

    }

    public void selectRecordsFromDbStudent(String selectTableSQL) throws SQLException{

    try {

    Connection dbConnection = null;

    Statement statement = null;

    //String selectTableSQL = "SELECT * FROM DB2ADMIN.\"student\"";

    dbConnection = connectDB();

    statement = dbConnection.createStatement();

    //System.out.println(selectTableSQL);

    ResultSet rs = statement.executeQuery(selectTableSQL);

    while (rs.next()) {

    System.out.println("get records");

    }

    }catch (SQLException e) {

    System.out.println(e.getMessage());

    }

    }

    }

    相关文章

      网友评论

          本文标题:2018-01-11

          本文链接:https://www.haomeiwen.com/subject/botznxtx.html