JDBC例子1

 简单的从MySQL里读取值,显示到JTextField里

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication35;

import java.awt.BorderLayout;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.sql.*;
/**
 *
 * @author 91152
 */
public class JavaApplication35 {

    /**
     * @param args the command line arguments
     */
    JFrame jf = new JFrame();
    JButton bt = new JButton("点击我");
    JTextField jt1 = new JTextField(20);
    JTextField jt2 = new JTextField(20);
    Box box = Box.createVerticalBox();
    
    String driver;
    String url;
    String user;
    String pass;
    String abcd;
    String efgh;
    
    ResultSet rs;
    
    public void init(){
        bt.addActionListener(e -> {
            try{
                rs.next();
                abcd = rs.getString(2);
                efgh = rs.getString(3);
            }
            catch(SQLException sq){
                sq.printStackTrace();
            }
            
            jt1.setText(abcd);
            jt2.setText(efgh);
        });
        jt1.setText(abcd);
        jt2.setText(efgh);
        box.add(bt);
        box.add(jt1);
        box.add(jt2);
        jf.add(box);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }
    
    public void getParam(String param) throws Exception{
        Properties prop = new Properties();
        prop.load(new FileReader(param));
        driver = prop.getProperty("driver");
        url = prop.getProperty("url");
        user = prop.getProperty("user");
        pass = prop.getProperty("pass");
    }
    
    public void createTable() throws Exception{
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,user,pass);
        Statement stat = conn.createStatement();
        rs = stat.executeQuery("SELECT * FROM test_1.a333;");
        rs.next();
        abcd = rs.getString(2);
        efgh = rs.getString(3);
        System.out.println(abcd);
    }
    public static void main(String[] args) throws Exception {
        JavaApplication35 ja = new JavaApplication35();
        ja.getParam("mysql.ini");
        ja.createTable();
        ja.init();
        
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值