class
www
{
public
static
void
main(String[]args)
throws
Exception
{
//第一步:加载驱动(驱动jar包必须加入classpath中)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
//第二步:建立连接(根据实际情况替换数据库的主机地址、端口号、数据库明、登录名、密码)
Connection
conn
=
DriverManager.getConnection("jdbc:sqlserver://localhost:1433databaseName=shijiuban1",
"sa",
"111111")
System.out.println("当前连接到的数据库="+conn.getCatalog())//查看当前连接到的数据库名
//第三步:创建Statement对象
Statement
stmt
=
conn.createStatement()//只读的结果集
//Statement
stmt2
=
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE)//可更新的结果集
//第四步:执行 *** 作(增删改查)
ResultSet
rs
=
stmt.executeQuery("select
*
from
stt")
//处理结果
while(rs.next()){
System.out.println(rs.getString("1"))
}
//第五步:关闭连接
conn.close()
}
}
ConnectionManager.javaimport java.sql.*
public class ConnectionManager {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver"
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433DatabaseName=student"
private static final String DATABASE_USER="xz"
private static final String DATABASE_PASSWORD="test"
private ConnectionManager()
{}
/**
* 返回连接
* @return Connection
* @throws DBAccessException
*/
public static Connection getConnection()
{
Connection dbConnection=null
try {
Class.forName(DRIVER_CLASS)
dbConnection=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD)
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return dbConnection
}
/**
* 关闭连接
* @param conn
* Connection
*/
public static void closeConnection(Connection conn)
{
try{
if(conn!=null &&!conn.isClosed())
{
conn.close()
conn=null
}
}catch(Exception ex)
{
System.out.println(ex.getMessage())
}
}
/**
* 关闭结果集
* @param rs
* ResultSet
*/
public static void closeResultSet(ResultSet rs)
{
try{
if(rs!=null)
{
rs.close()
rs=null
}
}catch(Exception ex)
{
System.out.println(ex.getMessage())
}
}
/**
* 关闭SQL预处理语句
* @param pstmt
* PreparedStatement
*/
public static void closePreparedStatement(PreparedStatement pstmt)
{
try{
if(pstmt!=null)
{
pstmt.close()
pstmt=null
}
}catch(Exception ex)
{
System.out.println(ex.getMessage())
}
}
/**
* 关闭SQL语句
* @param stmt
* Statement
*/
public static void closeStatement(Statement stmt)
{
try{
if(stmt!=null)
{
stmt.close()
stmt=null
}
}catch(Exception ex)
{
System.out.println(ex.getMessage())
}
}
public static void main(String[]args)
{
try {
Connection con=ConnectionManager.getConnection()
if(!con.isClosed())
System.out.println("数据库连接成功。。。。。")
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
User.javapublic class User {
private String username
private String userpass
public String getUsername() {
return username
}
public void setUsername(String username) {
this.username = username
}
public String getUserpass() {
return userpass
}
public void setUserpass(String userpass) {
this.userpass = userpass
}
}
UserDAO.javaimport java.util.*
import java.sql.*
public class UserDAO {
public List<User>getAllUser()
{
Connection conn=null
PreparedStatement pstmt=null
ResultSet rs=null
List<User>userList=new ArrayList<User>()
String sql="select UserName,UserPas from Users"
try {
conn=ConnectionManager.getConnection()
pstmt=conn.prepareStatement(sql)
rs=pstmt.executeQuery()
while(rs.next())
{
User u=new User()
u.setUsername(rs.getString("UserName"))
u.setUserpass(rs.getString("UserPas"))
userList.add(u)
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}finally
{
ConnectionManager.closeConnection(conn)
ConnectionManager.closePreparedStatement(pstmt)
ConnectionManager.closeResultSet(rs)
}
return userList
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)