JDBC_01

使用JDBC_执行DDL以及DML语句的insert操作

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver"); //导包
//获取一个连接对象
Connection connection = DriverManager.getConnection(url:"jdbc:mysql://localhost:3306/myee_2204",user:"root",password:"******");
//准备sql语句
String sql = "insert into **(id,name,...)value(1,'喜洋洋',...);
//通过数据库的连接获取数据库的执行对象
Statement statement = connection.creatStatement();
//通过执行对象执行sql语句,将sql语句发送给数据库,(DDL语句/DML语句)
int count = statement.executeUpdate(sql);
System.out.println("影响了"+count+"行");
//释放资源
connection.close();
statement.close();
}

jdbc7大步骤

1.导入驱动jar包

2.注册驱动

Class.forName("com.mysql.jdbc.Driver");
这样写的目的是为了保证向后兼容性

3.获取数据库的连接对象

Connection conn = DriverManager.getConnection();

4.准备sql语句

String sql = "create table 表名(xxx 字段类型,...)";//ddl语句,数据库定义语句
//DML语句:数据库操作语句
String sql = "delete from 表名 where 条件";
String sql = "insert into...";
String sql = "update 表名 set 字段名称 = 值 where 条件";

5.通过数据库的连接对象获取执行对象

Statement stmt = Connection数据库的连接对象.createStatement();

6.执行sql语句 DML语句:举例

String sql = insert into account(name,balance)values('赵又廷',1000);
//statement执行对象,将sql发送给数据库
int count = Statement接口对象.executeUpdate(sql);
//影响的行数...

7.释放资源

public class hiveBinaryTranslate extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Task<BufferedImage> queryTask = new Task<BufferedImage>() { @Override protected BufferedImage call() throws Exception { String CONNECTION_URL = "jdbc:hive2://192.168.88.100:10000/default"; String USERNAME = "root"; String PASSWORD = "123456"; String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver"; Class.forName(JDBC_DRIVER); Connection conn = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD); Statement stmt = conn.createStatement(); String sql = "SELECT data FROM picture_01 WHERE dt = '2023-07-13'"; ResultSet rs = stmt.executeQuery(sql); rs.next(); InputStream inputStream = rs.getBinaryStream("data"); BufferedImage bufferedImage = ImageIO.read(inputStream); rs.close(); stmt.close(); conn.close(); inputStream.close(); return bufferedImage; } }; queryTask.setOnSucceeded(event -> { BufferedImage bufferedImage = queryTask.getValue(); Image image = SwingFXUtils.toFXImage(bufferedImage, null); ImageView imageView = new ImageView(image); Scene scene = new Scene(imageView.getParent()); primaryStage.setTitle("Image Viewer"); primaryStage.setScene(scene); primaryStage.show(); }); queryTask.setOnFailed(event -> { Throwable exception = queryTask.getException(); // 处理异常 exception.printStackTrace(); }); Thread queryThread = new Thread(queryTask); queryThread.start(); } }报错Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85) at hiveBinaryTranslate.lambda$start$0(hiveBinaryTranslate.java:49) 如何修改
07-17
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值