最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Java链接mySQL数据库进行增删改查_MySQL

来源:懂视网 责编:小采 时间:2020-11-09 19:30:01
文档

Java链接mySQL数据库进行增删改查_MySQL

Java链接mySQL数据库进行增删改查_MySQL:Java链接mySQL数据库代码改和查对于增加是一样的public class JDBCTest { /** * ResultSet封装了JDBC结果集,进行查询的结果 */ @Test public void testResultSet() { Connection connection = null; Statement
推荐度:
导读Java链接mySQL数据库进行增删改查_MySQL:Java链接mySQL数据库代码改和查对于增加是一样的public class JDBCTest { /** * ResultSet封装了JDBC结果集,进行查询的结果 */ @Test public void testResultSet() { Connection connection = null; Statement

Java链接mySQL数据库代码

改和查对于增加是一样的

public class JDBCTest {	/**	 * ResultSet封装了JDBC结果集,进行查询的结果	 */	@Test	public void testResultSet() {	Connection connection = null;	Statement statement = null;	ResultSet rs = null;	try {	connection = JDBCTools.getConnection();	statement = (Statement) connection.createStatement();	String sql = "SELECT * FROM jdbc_1";	rs = statement.executeQuery(sql);	while (rs.next()) {	System.out.print(rs.getInt(1) + "/t");	System.out.print(rs.getString(2) + "/t");	System.out.print(rs.getString(3));	System.out.println();	}	} catch (Exception e) {	e.printStackTrace();	} finally {	JDBCTools.release(rs, statement, connection);	}	}	/**	 * 通用更新方法	 * @throws SQLException 	 */	public void updata(String sql) throws SQLException {	Connection connection = null;	Statement statement = null;	try {	connection = getConnection();	// 获取Statement 对象	statement = (Statement) connection.createStatement();	// 执行sql语句	statement.executeUpdate(sql);	} catch (Exception e) {	// TODO Auto-generated catch block	e.printStackTrace();	} finally {	try {	// 关闭statement对象	if (statement != null) {	statement.close();	}	} catch (Exception e) {	// TODO Auto-generated catch block	e.printStackTrace();	} finally {	// 关闭数据库连接	if (connection != null) {	connection.close();	}	}	}	}	/**	 * 1.通过jdbc向指定表中插入数据	 * 	 * @throws Exception	 * 	 */	@Test	public void testStatement() throws Exception {	// 获取数据库链接	Connection connection = null;	Statement statement = null;	try {	connection = getConnection();	// 要执行的sql语句	String sql = "INSERT INTO jdbc_1 (jname,addr) VALUE ('李力','浑江市')";	// 获取Statement 对象	statement = (Statement) connection.createStatement();	// 执行sql语句	statement.executeUpdate(sql);	} catch (Exception e) {	// TODO Auto-generated catch block	e.printStackTrace();	} finally {	try {	// 关闭statement对象	if (statement != null) {	statement.close();	}	} catch (Exception e) {	// TODO Auto-generated catch block	e.printStackTrace();	} finally {	// 关闭数据库连接	if (connection != null) {	connection.close();	}	}	}	}	/**	 * DriverManager类是驱动的管理类 利用DriverManager连接数据库,进行数据库驱动注册	 * 	 * @throws Exception	 */	@Test	public void testDriverManager() throws Exception {	String driverClass = null;	String jdbcUrl = null;	String user = null;	String password = null;	// 读取properties文件	InputStream in = getClass().getClassLoader().getResourceAsStream(	"jdbc.properties");	Properties properties = new Properties();	properties.load(in);	driverClass = properties.getProperty("driver");	jdbcUrl = properties.getProperty("url");	user = properties.getProperty("user");	password = properties.getProperty("password");	Class.forName(driverClass);	Connection connection = (Connection) DriverManager.getConnection(	jdbcUrl, user, password);	System.out.println(connection);	}	/**	 * 驱动测试,对链接驱动进行测试	 * 	 * @throws SQLException	 * 	 */	@Test	public void testDriver() throws SQLException {	// 创建Drivers实现类	Driver driver = new com.mysql.jdbc.Driver();	String url = "jdbc:mysql://localhost:3306/jdbc";	Properties info = new Properties();	info.put("user", "root");	info.put("password", "root");	// 调用driver接口的Connection	Connection connection = (Connection) driver.connect(url, info);	System.out.println(connection);	}	/**	 * 编写通用方法获取任意数据库链接,不用修改源程序	 * 	 * @throws ClassNotFoundException	 * @throws IllegalAccessException	 * @throws InstantiationException	 * @throws SQLException	 * @throws IOException	 */	public Connection getConnection() throws InstantiationException,	IllegalAccessException, ClassNotFoundException, SQLException,	IOException {	String driverClass = null;	String jdbcUrl = null;	String user = null;	String password = null;	// 读取properties文件	InputStream in = getClass().getClassLoader().getResourceAsStream(	"jdbc.properties");	Properties properties = new Properties();	properties.load(in);	driverClass = properties.getProperty("driver");	jdbcUrl = properties.getProperty("url");	user = properties.getProperty("user");	password = properties.getProperty("password");	Driver driver = (Driver) Class.forName(driverClass).newInstance();	Properties info = new Properties();	info.put("user", user);	info.put("password", password);	Connection connection = (Connection) driver.connect(jdbcUrl, info);	return connection;	}	@Test	public void testConnection() throws InstantiationException,	IllegalAccessException, ClassNotFoundException, SQLException,	IOException {	System.out.println(getConnection());	}}
public class JDBCTools {	/**	 * 结果查询关闭	 * @param rs	 * @param statement	 * @param conn	 */	public static void release(ResultSet rs,Statement statement, Connection conn) {	if (rs != null) {	try {	rs.close();	} catch (SQLException e) {	// TODO Auto-generated catch block	e.printStackTrace();	}	}	if (statement != null) {	try {	statement.close();	} catch (Exception e2) {	e2.printStackTrace();	}	}	if (conn != null) {	try {	conn.close();	} catch (Exception e2) {	e2.printStackTrace();	}	}	}	/**	 * 数据库更新方法	 * @param sql	 */	public void uodate(String sql) {	Connection connection = null;	Statement statement = null;	try {	connection = JDBCTools.getConnection();	statement = (Statement) connection.createStatement();	statement.executeUpdate(sql);	} catch (Exception e) {	e.printStackTrace();	} finally {	JDBCTools.release(statement, connection);	}	}	/**	 * 关闭数据库连接的方法	 * @param statement	 * @param conn	 */	public static void release(Statement statement, Connection conn) {	if (statement != null) {	try {	statement.close();	} catch (Exception e2) {	e2.printStackTrace();	}	}	if (conn != null) {	try {	conn.close();	} catch (Exception e2) {	e2.printStackTrace();	}	}	}	/**	 * 编写通用方法获取任意数据库链接,不用修改源程序	 * 	 * @return	 * @throws ClassNotFoundException	 * @throws IllegalAccessException	 * @throws InstantiationException	 * @throws SQLException	 * @throws IOException	 */	public static Connection getConnection() throws InstantiationException,	IllegalAccessException, ClassNotFoundException, SQLException,	IOException {	String driverClass = null;	String jdbcUrl = null;	String user = null;	String password = null;	// 读取properties文件	InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(	"jdbc.properties");	Properties properties = new Properties();	properties.load(in);	driverClass = properties.getProperty("driver");	jdbcUrl = properties.getProperty("url");	user = properties.getProperty("user");	password = properties.getProperty("password");	Driver driver = (Driver) Class.forName(driverClass).newInstance();	Properties info = new Properties();	info.put("user", user);	info.put("password", password);	Connection connection = (Connection) driver.connect(jdbcUrl, info);	return connection;	}}

文档

Java链接mySQL数据库进行增删改查_MySQL

Java链接mySQL数据库进行增删改查_MySQL:Java链接mySQL数据库代码改和查对于增加是一样的public class JDBCTest { /** * ResultSet封装了JDBC结果集,进行查询的结果 */ @Test public void testResultSet() { Connection connection = null; Statement
推荐度:
标签: 进行 数据库 java
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top