⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conn.java

📁 java语言开发
💻 JAVA
字号:
package com.yourcompany.struts;
import java.sql.*;

public class Conn {
	public String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
	public String dbUrl="jdbc:odbc:bookShop";
	private String userName="";
	private String userPwd="";
	public Connection conn;
	public Statement st;
	private Pageable rs;
	
	//连接数据库,执行语句,返回得到的记录项--用于查询
	public void init(String sql) throws Exception
	{
		Class.forName(driverName);
		conn=DriverManager.getConnection(dbUrl, null,null);
		st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		rs=new PageableResultSet2(st.executeQuery(sql));
	}
	
	//判断记录欺罔是否为空,若为空则调用函数 init(sql)
	public Pageable getRs(String sql) throws Exception
	{
		if(rs==null) 
		{
			init(sql);
		}
		return rs;
	}
	
	//执行 添加、更新、删除,无返回
	public void exeUpd(String sql) throws Exception
	{
		Class.forName(driverName);
		conn=DriverManager.getConnection(dbUrl,null,null);
		st=conn.createStatement();
		st.executeUpdate(sql);
	}
	
	//关闭 查询 连接
	public void closeRs() throws Exception
	{
		rs.close();
		st.close();
		conn.close();
	}
	
	//关闭 添加、更新、删除 连接
	public void closeNoRs() throws Exception
	{
		st.close();
		conn.close();
	}
	public static void main(String[] args)
	{
		Connection conn = null;
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
			System.out.println("正确");
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("连接问题1");
		}
		try {
			conn = DriverManager.getConnection("jdbc:odbc:bookShop", null,null);
			System.out.println("正确");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("连接问题2");
		}
		try {
			Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			System.out.println("正确");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("连接问题3");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -