JDBC学习笔记(5)之PreparedStatement、ResultSetMetaData实现

</pre><pre name="code" class="java"><span style="font-size:18px;">(1)建立连接;将驱动加载、url,usrname,passwd封装进properties文件里</span>
</pre><pre name="code" class="java">package JdbcPackage;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcGetConnection {
	private static Properties pro=new Properties();
	private static Connection conn=null;
	private static JdbcTemplate obj=null;
	public static JdbcTemplate getConnection()
	{
		if(obj==null)
		{
			obj=new JdbcTemplate();
			try {
				pro.load(new FileInputStream("jdbc.properties"));
				Class.forName(pro.getProperty("jdbc.driver"));
				conn=DriverManager.getConnection(pro.getProperty("jdbc.url"), 
						pro.getProperty("jdbc.usrName"), pro.getProperty("jdbc.passwd"));
			} catch (IOException | ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			obj.setConnection(conn);
		}
		return obj;
		
		
	}

}
</pre><pre name="code" class="java"><span style="font-size:18px;">(2)建立与封装的连接数据库类之间的联系</span>
public void setConnection(Connection connection)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>conn=connection;
<span style="white-space:pre">	</span>}
</pre><pre name="code" class="java"><span style="font-size:18px;">(3)实现</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">Statement、</span><span style="font-family: Arial, Helvetica, sans-serif;font-size:18px;">PreparedStatement下的executeQuery、executeUpdate;</span>
/***************************Statement下executeQuery***********************************************/<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public  ResultSet executeQueryStatement(String sql)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if(st!=null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>st.close();
<span style="white-space:pre">				</span>st=null;
<span style="white-space:pre">			</span>} catch (SQLException e) {
<span style="white-space:pre">				</span>// TODO Auto-generated catch block
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(conn==null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>st=conn.createStatement();
<span style="white-space:pre">			</span>rs=st.executeQuery(sql);
<span style="white-space:pre">		</span>} catch (SQLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return rs;
<span style="white-space:pre">	</span>}
</pre><pre name="code" class="java">/*****************************Statement下executeUpdate*********************************************/
public int executeUpdateStatement(String sql)
{
int num = 0;
if(st!=null)
{
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>st.close();
<span style="white-space:pre">				</span>st=null;
<span style="white-space:pre">			</span>} catch (SQLException e) {
<span style="white-space:pre">				</span>// TODO Auto-generated catch block
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>return 0;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(conn==null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>return 0;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>st=conn.createStatement();
<span style="white-space:pre">			</span>num=st.executeUpdate(sql);
<span style="white-space:pre">		</span>} catch (SQLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return num;
<span style="white-space:pre">	</span>}
/*****************PreparedStatement下查询*********************************************************/
<span style="white-space:pre">	</span>public PreparedStatement executeQuerySelect()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if(conn==null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/*String sql="SELECT";
<span style="white-space:pre">		</span>sql=sql+pro.getProperty("mysql.select")+"FROM";
<span style="white-space:pre">		</span>sql=sql+pro.getProperty("mysql.table");*/
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>pre=conn.prepareStatement("SELECT * FROM STUDENT1");
<span style="white-space:pre">		</span>} catch (SQLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return pre;
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
/***********************PreparedStatement下插入***************************************************/
<span style="white-space:pre">	</span>public PreparedStatement executeUpdateInsert()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if(conn==null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>//这种为什么会错?!
//<span style="white-space:pre">		</span>String sql="INSERT INTO";
//<span style="white-space:pre">		</span>sql=pro.getProperty("mysql.table")+"VALUES(?,?,?,?)";
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>pre=conn.prepareStatement("INSERT INTO STUDENT1 VALUES(?,?,?,?)");
<span style="white-space:pre">		</span>} catch (SQLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return pre;
<span style="white-space:pre">	</span>}
/****************************<span style="font-family: Arial, Helvetica, sans-serif;">PreparedStatement下删除</span><span style="font-family: Arial, Helvetica, sans-serif;">**********************************************/</span>
<span style="white-space:pre">	</span>public PreparedStatement executeUpdateDelete()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if(conn==null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/*String sql="DELETE FROM ";
<span style="white-space:pre">		</span>sql=sql+pro.getProperty("mysql.table");*/
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>pre=conn.prepareStatement("DELETE FROM STUDENT1");
<span style="white-space:pre">			</span>pre.executeUpdate();
<span style="white-space:pre">		</span>} catch (SQLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return pre;
<span style="white-space:pre">	</span>}
/******************************PreparedStatement下更新********************************************/
<span style="white-space:pre">	</span>public PreparedStatement executeUpdateUpdate()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>if(conn==null)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/*String sql="UPDATE "+pro.getProperty("mysql.table");
<span style="white-space:pre">		</span>sql=sql+" SET "+pro.getProperty("table.column")+" = ?";*/
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>pre=conn.prepareStatement("UPDATE STUDENT1 SET STUAGE=? WHERE STUID=?");
<span style="white-space:pre">			</span>//若在此处加pre.executeUpdate(),在运行程序是会报错
<span style="white-space:pre">		</span>} catch (SQLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return pre;
<span style="white-space:pre">	</span>}
<span style="font-size:18px;">(4)利用</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;">ResultSetMetaData实现table中数据打印</span></span><span style="font-size:18px;">
</span>
/************************输入表名即打印**************************************************/
<span style="white-space:pre">	</span>public void printTable(String tableName)
<span style="white-space:pre">	</span>{
//<span style="white-space:pre">		</span>String sql="SELECT * FROM "+tableName;
<span style="white-space:pre">		</span>PreparedStatement pre=executeQuerySelect();
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>rs=pre.executeQuery("SELECT * FROM "+tableName);
<span style="white-space:pre">				</span><span style="color:#ff0000;">ResultSetMetaData</span> rsm=rs.getMetaData();// 通过ResultSet创建ResultSetMetaDat对象
<span style="white-space:pre">				</span>for(int i=0;i<rsm.getColumnCount();i++)
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>System.out.print(rsm.getColumnName(i+1)+"\t");
<span style="white-space:pre">				</span>}
<span style="white-space:pre">				</span>System.out.println();
<span style="white-space:pre">				</span>while(rs.next())//不加会报Before start of result set
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>for(int i=0;i<rsm.getColumnCount();i++)
<span style="white-space:pre">					</span>{
<span style="white-space:pre">						</span>int type=rsm.getColumnType(i+1);
<span style="white-space:pre">						</span>switch(type)
<span style="white-space:pre">						</span>{
<span style="white-space:pre">							</span>case Types.VARCHAR:
<span style="white-space:pre">							</span>case Types.CHAR:
<span style="white-space:pre">								</span>System.out.print(rs.getString(i+1)+"\t");
<span style="white-space:pre">								</span>break;//不加会报Invalid value for getInt() - 'STU0
<span style="white-space:pre">							</span>case Types.INTEGER:
<span style="white-space:pre">							</span>case Types.SMALLINT:
<span style="white-space:pre">								</span>System.out.print(rs.getInt(i+1)+"\t");
<span style="white-space:pre">								</span>break;
<span style="white-space:pre">						</span>}
<span style="white-space:pre">					</span>}
<span style="white-space:pre">					</span>System.out.println();
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>} catch (SQLException e) {
<span style="white-space:pre">				</span>// TODO Auto-generated catch block
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值