jdbc相关知识

1,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(FRAME_X_ORIGN, FRAME_Y_ORIGN);

2, extends
3, Container contentPane = getContentPane();
FlowLayout flo = new FlowLayout();
contentPane.setLayout(flo);
4,添加文本框 JTextField,setColumns(22);
5,FlowLayout - 流式布局 将容器中的组件从左到右、从上到下的方式排列
6,GridLayout - 网格布局 将窗口划分为若干个单元格,然后将 组件顺次地放在这些单元格中 , setLayout(new GridLayout(5,1));
7 BorderLayout - 边界布局 将窗口划分为北、南、西、东、中五个方位,分别用 , 在添加组件时需要指定组件所处的方位 add(queding, BorderLayout.CENTER);
8,按钮的监听器是ActionListener,而复选框和单选按 钮的是ItemListener.
9,mysql开源,免费,轻量级
10, 列出数据库: show databases 4. 创建数据库: create database 数据库名 5. 删除数据库: drop database 数据库名 2. 进入MySQL: mysql -u 用户名 -p 用户密码 6. 选择数据库: use 数据库名
11,列出表: show tables;create 表名(字段名 数据类型[,字段名 数据类型]);列出表结构: describe 表名;插入数据: insert into 表名(字段列表) values(值列表)
12, 导入sql命令文件: source 文件名路径如: source d:/grade.sql
13,prepareStatement 创建预处理对象PreparedStatement
14,close() 释放此connection对象的数据库和JDBC资源
15,String url = “jdbc:mysql://localhost:3306/grade?user=root&” +“password=12345”;
16,try {
Class.forName(“com.mysql.jdbc.Driver”);
String url = “jdbc:mysql://localhost:3306/grade?user=root&” +
“password=123456”;
connection = DriverManager.getConnection(url);
if(connection!=null)
System.out.println(“数据库连接成功”);
} catch (ClassNotFoundException e) {
System.out.println(“JDBC driver error”);
} catch (SQLException e) {
System.out.println(“JDBC operation error”);
}
17,在应用程序中编写连接代码,连接代码包括:(1) 加载JDBC驱动程序;(2) 定义JDBC URL;(3) 调用连接类
18, PreparedStatement,, ResultSet
19,executeQuery(String sql)
20,executeUpdate(sql)
21,setInt(int index, int k)
22, 与PreparedStatement比较
PreperedStatement(从Statement扩展而来)相对
Statement的优点:
(1)没有SQL注入的问题。
(2)Statement会使数据库频繁编译SQL,可能造成数
据库缓冲区溢出。
(3)数据库和驱动可以对PreperedStatement进行优化
(只有在相关联的数据库连接没有关闭的情况下有效)
23 ,从各控件取出数据
类型转换 验证数据
连接数据库,插入数据
检查必填字段
插入成功或失败的处理
24,public class JDBCUtil {
public Connection getConn() {
Connection connection = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
String url = “jdbc:mysql://localhost:3306/grade?user=root&”
+ “password=liukun”;
connection = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
System.out.println(“JDBC driver error”);
} catch (SQLException e) {
System.out.println(“JDBC connection error”);
}
return connection;
}
}

		25,public void closeConn(ResultSet rs, Statement st, Connection conn){
			if(rs!=null){
			try { rs.close();
		} catch (SQLException e) { ... }
		}
			if(st!=null){
		try { st.close();
		} catch (SQLException e) { ... }
		}
			if(conn!=null){
			try { conn.close();
		} catch (SQLException e) { ... }
		}
		}

26,while(rs.next()){}
27,
代码部分:
1
public static void main(String []sdf)
{
Connection con = null;
ResultSet rs = null;
PreparedStatement preStmt = null;
try {
String url = “jdbc:mysql://localhost:3306/grade?user=root&password=root”;
Class.forName(“com.mysql.jdbc.Driver”);
//String url = “jdbc:mysql://localhost:3306/grade?user=root&password=123456”;
con = DriverManager.getConnection(url);
if(con != null)
{
System.out.println(“链接成功”);
}

				String sql = "select * from stock where orderId = 1";
				preStmt = con.prepareStatement(sql);
	preStmt.setInt(1, 1256);
				rs = preStmt.executeQuery();
		
		while(rs.next()) {
			System.out.println("订单号="+"  "+rs.getInt(1));
			System.out.println("货物名称="+"  "+rs.getString(2));
			System.out.println("交货日期="+"  "+rs.getInt(3));
			System.out.println("数量="+"  "+rs.getInt("count"));
			System.out.println("金额 ="+"  "+rs.getInt("money"));
			
		}

	} catch (ClassNotFoundException e) {
		System.out.println("driver error");
		e.printStackTrace();
	} catch (SQLException e) {
		System.out.println("operate error");
		e.printStackTrace();
	}
}

}
2
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub

			 if(event.getSource()instanceof JButton)
			 {
				 JButton clickedButton = (JButton) event.getSource();
				 if(clickedButton == addButton)
				 {
					 addText(inputLine.getText());
				 }
				 else
				 {
					 clearText();
				 }
			 }
			 
			 else
			 {
				 addText(inputLine.getText());
			 }	 
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值