码迷,mamicode.com
首页 > 数据库 > 详细

mysql jdbc连接

时间:2015-07-25 19:45:33      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

public class JDBCTest {

	public static void main(String[] args) {
		String sql = "SELECT * FROM usertest";
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wzz","root","1020");
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next()){
				System.out.println(rs.getString("name")+rs.getInt("age"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
			} catch (Exception e2) {
			}
			try {
				st.close();
			} catch (Exception e3) {
			}
			try {
				conn.close();
			} catch (Exception e4) {
			}
		}
	}
}




public class JDBCTest {

	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wzz","root","1020");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	
	public static void main(String[] args) {
		Connection conn = getConnection();
		Statement st = null;
		try {
			String sql="insert into usertest(name,age) values(‘abc‘,18)";
			st = conn.createStatement();
			int count = st.executeUpdate(sql);
			System.out.println(count);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				st.close();
			} catch (Exception e1) {
			}
			try {
				conn.close();
			} catch (Exception e2) {
			}
		}
	}
}

 

  

mysql jdbc连接

标签:

原文地址:http://www.cnblogs.com/wzz1020/p/4676372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!