window下的java程序连接linux系统的mysql数据库

实现的功能:window下连接linux的mysql数据库

准备的环境

windows1.已经编写好的连接数据库的java测试程序:

package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* ResultSet结果集的基本用法(executeQuery()将会返回ResultSet结果集)
* @author zanzan
*
*/
public class Demo2 {
public static void main(String[] args){

Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {

Class.forName("com.mysql.jdbc.Driver");
long start = System.currentTimeMillis();
conn = DriverManager.getConnection("jdbc:mysql://与linux的ip映射的域名:映射的端口号/数据库名称", "mysql数据库登陆名", "密码");

System.out.println(conn);

//进行数据库的查询
String sql = "select id,name from test where id>?";//?表示占位符

//建立PraparedStatemen对象进行SQL静态语句的操作,同时返回PraparedStatememnt对象,导入PraparedStatement包
ps = conn.prepareStatement(sql);

ps.setObject(1, 0);

rs = ps.executeQuery();

//将结果集进行遍历

while(rs.next()){
System.out.println(rs.getInt(1)+"---"+rs.getString(2));
}
long end = System.currentTimeMillis();
System.out.println("建立与数据库的连接所需要的时间"+(end-start)+"ms");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

//关闭顺序是按照先开后关的原则
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一些代码示例,帮助您开始编写Java资产管理系统窗体并连接MySQL数据库。 首先,您需要下载并安装MySQL数据库,然后创建一个名为“asset_management”的数据库。在该数据库中,您需要创建一个名为“assets”的表,其中包含以下列:id(INT,主键,自动增加),name(VARCHAR),description(VARCHAR),value(DOUBLE)和acquisition_date(DATE)。 接下来,您需要使用Java连接MySQL数据库。这可以通过使用Java JDBC驱动程序来实现。以下是一个示例代码: ```java import java.sql.*; public class MySQLConnector { private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; private static final String DB_URL = "jdbc:mysql://localhost/asset_management"; private static final String USER = "your_username"; private static final String PASS = "your_password"; public static Connection getConnection() { Connection conn = null; try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); } catch (Exception e) { e.printStackTrace(); } return conn; } } ``` 这个类使用了静态方法`getConnection()`来连接MySQL数据库。您需要将`your_username`和`your_password`分别替换为您的MySQL用户名和密码。 接下来,您可以编写一个Java类来表示资产,并使用该类在Java窗体中显示和管理资产。以下是一个示例代码: ```java import java.util.Date; public class Asset { private int id; private String name; private String description; private double value; private Date acquisitionDate; public Asset(int id, String name, String description, double value, Date acquisitionDate) { this.id = id; this.name = name; this.description = description; this.value = value; this.acquisitionDate = acquisitionDate; } public int getId() { return id; } public String getName() { return name; } public String getDescription() { return description; } public double getValue() { return value; } public Date getAcquisitionDate() { return acquisitionDate; } } ``` 这个类表示一个资产,并包含`id`,`name`,`description`,`value`和`acquisitionDate`属性。您可以使用这个类来在Java窗体中显示和管理资产。 最后,您需要编写Java窗体以显示和管理资产。以下是一个示例代码: ```java import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Date; public class AssetManagementSystem extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static final String[] COLUMNS = {"ID", "Name", "Description", "Value", "Acquisition Date"}; private static final String[] FIELD_NAMES = {"Name", "Description", "Value", "Acquisition Date (YYYY-MM-DD)"}; private JTextField[] fields; private JButton addButton; private JTable table; private DefaultTableModel model; private ArrayList<Asset> assets; public AssetManagementSystem() { super("Asset Management System"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // Create fields for asset details JPanel fieldPanel = new JPanel(new GridLayout(FIELD_NAMES.length, 2)); fields = new JTextField[FIELD_NAMES.length]; for (int i = 0; i < FIELD_NAMES.length; i++) { fieldPanel.add(new JLabel(FIELD_NAMES[i])); fields[i] = new JTextField(); fieldPanel.add(fields[i]); } add(fieldPanel, BorderLayout.NORTH); // Create add button addButton = new JButton("Add Asset"); addButton.addActionListener(this); add(addButton, BorderLayout.SOUTH); // Create table to display assets model = new DefaultTableModel(COLUMNS, 0); table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane, BorderLayout.CENTER); // Load assets from database assets = new ArrayList<>(); try { Connection conn = MySQLConnector.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM assets"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String description = rs.getString("description"); double value = rs.getDouble("value"); Date acquisitionDate = rs.getDate("acquisition_date"); Asset asset = new Asset(id, name, description, value, acquisitionDate); assets.add(asset); model.addRow(new Object[] {id, name, description, value, acquisitionDate}); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } // Configure window setSize(800, 600); setVisible(true); } public static void main(String[] args) { new AssetManagementSystem(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) { try { Connection conn = MySQLConnector.getConnection(); PreparedStatement stmt = conn.prepareStatement("INSERT INTO assets (name, description, value, acquisition_date) VALUES (?, ?, ?, ?)"); stmt.setString(1, fields[0].getText()); stmt.setString(2, fields[1].getText()); stmt.setDouble(3, Double.parseDouble(fields[2].getText())); stmt.setDate(4, Date.valueOf(fields[3].getText())); stmt.executeUpdate(); stmt.close(); conn.close(); Asset asset = new Asset(assets.size() + 1, fields[0].getText(), fields[1].getText(), Double.parseDouble(fields[2].getText()), Date.valueOf(fields[3].getText())); assets.add(asset); model.addRow(new Object[] {asset.getId(), asset.getName(), asset.getDescription(), asset.getValue(), asset.getAcquisitionDate()}); for (JTextField field : fields) { field.setText(""); } } catch (Exception ex) { ex.printStackTrace(); } } } } ``` 这个窗体使用`Asset`类表示资产,并在窗体中显示和管理资产。窗体包括一个表格来显示资产,一个文本框来输入新资产的详细信息,以及一个“添加资产”按钮来将新资产添加到数据库和表格中。 当用户单击“添加资产”按钮时,窗体将连接MySQL数据库并将新资产添加到数据库和表格中。如果发生任何错误,窗体将打印出堆栈跟踪以帮助您调试代码。 希望这个示例代码可以帮助您开始编写Java资产管理系统窗体并连接MySQL数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值