dbbean.java

来自「一个JSP写的学生选课系统」· Java 代码 · 共 64 行

JAVA
64
字号
package test;

import java.sql.*;
import java.util.*;
import java.io.*;
import javax.sql.DataSource;

public class DbBean implements Serializable
{
	private static Connection con;
	
	public DbBean()
	{
	}

	public static synchronized Connection getCon() throws Exception
	{
		if (con == null)
		{
			try
			{
				Class.forName("org.gjt.mm.mysql.Driver").newInstance();
				String url = "jdbc:mysql://localhost/test";
				con = DriverManager.getConnection(url, "root", "200405");
				return con;
			}
			catch (Exception e)
			{
				throw e;
			}
		}
		return con;
	}

	public ResultSet getRs(String sql) throws Exception
	{
		try
		{
			con = getCon();
			Statement stmt = con.createStatement();
			return stmt.executeQuery(sql);
		}
		catch (Exception e)
		{
			throw e;
		}
	}

	public boolean Update(String sql) throws Exception
        {
		try
		{
			con = getCon();
			Statement stmt = con.createStatement();
			if (stmt.executeUpdate(sql) == 1)
			   return true;
			return false;
		}
		catch (Exception e)
		{
	             throw e;
		}	
	}
}

⌨️ 快捷键说明

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