首页 > 数据库技术 > 详细

mysql jdbc连接

时间:2015-07-25 19:42:23      阅读:278      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!