链接docker容器中的mysql数据库的java代码

前提准备

首先你要在docker中启动mysql容器,并且暴露3306端口。

中间准备

package delete;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Stmessege1 extends JFrame {
    private static final long serialVersionUID = 1L;
    private JTable table;// 声明表格

    public Vector getDataVector() throws SQLException, ClassNotFoundException {
        Connection conn = null;
        java.sql.Statement stmt = null;
        Class.forName("com.mysql.jdbc.Driver");
        String dburl = "jdbc:mysql://192.168.1.30:3306/mydb";
        String dbuser = "root";
        String dbpassword = "root";
        conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
        stmt = conn.createStatement();
        String sql = "select* from student";
        ResultSet rs = stmt.executeQuery(sql);
        Vector dataVector = new Vector();

        while(rs.next()) {
            Vector rowVector = new Vector();
             {
                    rowVector.add(rs.getString("sno"));
                    rowVector.add(rs.getString("sname"));
                    rowVector.add(rs.getString("sex"));
                    rowVector.add(rs.getString("age"));

            }
            dataVector.add(rowVector);

        }
        return dataVector;
    }
    public Vector getColumnVector() {
        Vector columnVector = new Vector();
        columnVector.add("sno");
        columnVector.add("sname");
        columnVector.add("sex");
        columnVector.add("age");
        return columnVector;
    }
    public Stmessege1() throws SQLException, ClassNotFoundException {
        super();
        // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Connection conn = null;
        Statement stmt = null;
        Class.forName("com.mysql.jdbc.Driver");
        String dburl = "jdbc:mysql://192.168.1.30:3306/mydb";
        String dbuser = "root";
        String dbpassword = "root";
        conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
        java.sql.Statement st = conn.createStatement();
        String sql = "select* from student";
        ResultSet rs = st.executeQuery(sql);
        Vector columnVector = getColumnVector();
        Vector dataVector = getDataVector();
        System.out.println(dataVector.toString());
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        table = new JTable(dataVector, columnVector);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setFont(new Font("",0,16));
        scrollPane.setViewportView(table);


        setTitle("连接远程docker数据库显示");
        setBounds(100, 100, 382, 223);
        setVisible(true);
    }

    public static void main(String Args[]) throws ClassNotFoundException, SQLException{
        new Stmessege1();

    }
}

运行结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值