package com.qf.utils;
import com.mysql.jdbc.Driver;
import com.qf.domain.User1;
import javax.swing.*;
import java.sql.*;
public class TestInsert {
public static void main(String[] args) {
try {
Driver d = new Driver();
DriverManager.registerDriver(d);
String url = "jdbc:mysql://localhost:3306/servlet";
String name = "root";
String password = "root";
String uid = JOptionPane.showInputDialog("请输入需要插入Id");
String firstname = JOptionPane.showInputDialog("请输入需要插入Firstname:");
String lastname = JOptionPane.showInputDialog("请输入需要插入lastname:");
firstname.replaceAll("\"","\'");
lastname.replaceAll("\"","\'");
int id = Integer.parseInt(uid);
String sql = "insert into user1(id,firstname,lastname)values(?,?,?)";
Connection connection = DriverManager.getConnection(url,name,password);
PreparedStatement ps = connection.prepareStatement(sql);
User1 user1 = new User1();
ps.setObject(1,id);
ps.setObject(2,firstname);
ps.setObject(3,lastname);
int result = ps.executeUpdate();
if (result >0){
JOptionPane.showMessageDialog(null,"数据录入成功","insert",2);
}
System.out.println(id+"\t"+firstname+"\t"+lastname);
// ps.close();
// connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java JDBC 简单插入
最新推荐文章于 2025-04-19 17:11:27 发布