首页 > 学院 > 开发设计 > 正文

JDBC例子

2019-11-06 06:22:33
字体:
来源:转载
供稿:网友
package dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class BaseDao {		//连接数据库的方法	public Connection getConn() throws Exception{		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");		String url="jdbc:sqlserver://localhost:1433;"			+ "DatabaseName=scmng";		String user="sa";		String passWord="yuyu668668";		return DriverManager.getConnection(url, user, password);	}		//关闭数据库的方法	public void closeAll(Connection conn,Statement stmt,ResultSet rs){				if(rs!=null){			try {				rs.close();			} catch (SQLException e) {				// TODO Auto-generated catch block				e.PRintStackTrace();			}		}		if(stmt!=null){			try {				stmt.close();			} catch (SQLException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}		if(conn!=null){			try {				conn.close();			} catch (SQLException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}	}}
package entity;public class Student {	private String id;	private String sno;	private String name;	private String gender;	public Student() {		super();	}	public Student(String sno, String name, String gender) {		super();		this.sno = sno;		this.name = name;		this.gender = gender;	}	public Student(String id, String sno, String name, String gender) {		super();		this.id = id;		this.sno = sno;		this.name = name;		this.gender = gender;	}	public String getId() {		return id;	}	public void setId(String id) {		this.id = id;	}	public String getSno() {		return sno;	}	public void setSno(String sno) {		this.sno = sno;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getGender() {		return gender;	}	public void setGender(String gender) {		this.gender = gender;	}}
package dao;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import entity.Student;public class StudentDao extends BaseDao{		//查询所有的方法	public List<Student> findAll(){				List<Student> list=new ArrayList<Student>();		String sql="select * from student";		Student stu=null;				Connection conn=null;		Statement stmt=null;		ResultSet rs=null;				try {			conn=this.getConn();			stmt=conn.createStatement();			rs=stmt.executeQuery(sql);						while(rs.next()){				stu=new Student(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));				list.add(stu);			}					} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();		}finally{			this.closeAll(conn, stmt, rs);		}				return list;	}}
package test;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import com.MySQL.jdbc.Connection;public class TestJDBC {	public static void main(String[] args) throws Exception {		//1.加载驱动包		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");		System.out.println("驱动加载成功!");				//2.连接数据库		String url="jdbc:sqlserver://localhost:1433;"			+ "DatabaseName=scmng";		String user="sa";		String PSD="yuyu668668";		java.sql.Connection conn=DriverManager.getConnection(url, user, psd);		System.out.println("数据库连接成功!");				//3.语句		String sql="select * from student";		Statement stmt=conn.createStatement();		System.out.println("语句获取成功");				//4.结果集		ResultSet rs=stmt.executeQuery(sql);		System.out.println("执行查询成功!");				//5.处理结果集,使用循环处理		//rs.next()指针乡下移动一条语句,如果到达最后一条后面,则结果为FALSE,循环结束		while(rs.next()){			System.out.println(rs.getString(1)+"/t"+rs.getString(2)+"/t"+		rs.getString(3)+"/t"+rs.getString(4));		}				//6.关闭结果集		rs.close();		//7.关闭语句		stmt.close();		//8.关闭连接		conn.close();		}}
上一篇:单调递增最长子序列

下一篇:随机数

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表