JDBC连接实例

 1 package com.javaee.corejava;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 
 8 public class JDBCConnect {
 9 
10     public JDBCConnect() {
11         // TODO Auto-generated constructor stub
12     }
13 
14     public static void main(String[] args) {
15         try {
16             //加载mysql的JDBC驱动
17             Class.forName("com.mysql.jdbc.Driver");
18             String url="jdbc://localhost:3306/v3x";
19             String userName="root";
20             String pwd="123456";
21             //获取数据库连接
22             Connection con= DriverManager.getConnection(url, userName, pwd);
23             String sql="select * from ctp_affair where id=?";
24             //Statement stmt=con.createStatement();
25             PreparedStatement pstmt=con.prepareStatement(sql);
26             pstmt.setLong(1, 0L);
27             //执行查询
28             ResultSet rs=pstmt.executeQuery();
29             while(rs.next()){
30                 String subject=rs.getString("subject");
31             }
32             //关闭连接,释放资源
33             rs.close();// 关闭记录集   
34             pstmt.close();// 关闭声明   
35             con.close();// 关闭连接对象   
36         } catch (Exception e) {
37             e.printStackTrace();
38         }
39         
40 
41     }
42 
43 }

 

转载于:https://www.cnblogs.com/miaoyf/p/4060955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值