jdbc连接mysql数据库

PHPz
풀어 주다: 2018-09-30 11:31:57
원래의
1253명이 탐색했습니다.

jdbc驱动到官网下载,放在jdk的相关目录下面,或者jar文件加入到工程下面 package test_mysql;import java.sql.*;import java.util.Set;public class testjdbc {public static Connection getConnection() throws ClassNotFoundException, SQLException{Stri

jdbc驱动到官网下载,放在jdk的相关目录下面,或者jar文件加入到工程下面

package test_mysql;

import java.sql.*;

import java.util.Set;

public class testjdbc {

	public static Connection getConnection() throws ClassNotFoundException, SQLException{

		String URL="jdbc:mysql://localhost:3306/employees";

		//加载驱动程序

		Class.forName("com.mysql.jdbc.Driver");

		String user="root";

		String passwd="";

		//默认密码为空

		//还有选择数据库,哪个数据

		//DriverManager.

		//Connection con=DriverManager.getConnection(URL, user, passwd);

		//Connection con=DriverManager.getConnection(url, user, password);

		String test_url="";

		//String my_url="jdbc:mysql://localhost:3306;DatabaseName=employees;User=root;Password=";

		//test_url+=URL+3306+";"+"DatabaseName=employees;"+"User=root;"+"Password="+"";

		Connection con=DriverManager.getConnection(URL,user,passwd);

		return con;

	}

	public static void main(String[] args){

		String sql="select * from  departments";

		Statement statement=null;

		Connection con=null;

		ResultSet set=null;

		try {

			con = getConnection();

			//con.set

			if(con==null)

				System.out.println("con null");

			statement=con.createStatement();

			set=statement.executeQuery(sql);

			//int n=set.getFetchSize();

			//System.out.println(n);

			while(set.next()){

				System.out.println(set.getString(1)+"  "+set.getString(2));

			}

		} catch (ClassNotFoundException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (SQLException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}finally{

	  if(con!=null)

		try {

			set.close();

			statement.close();

			con.close();

			

		} catch (SQLException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		}

		

	}

}
로그인 후 복사

更多相关教程请访问 MySQL视频教程

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!