jdbc 连接 mysql数据库

来源:互联网 发布:indexof js 数组 编辑:程序博客网 时间:2024/05/15 19:50

要注意两点:

1下载JDBC驱动mysql-connector-java-5.0.5-bin.jar导入到工程中去
工程(右键)--buildpath--configure build path--add external jars。
2 url的写法:“jdbc:mysql://主机IP或主机名:3306/数据库名”
  主机ip或主机名查找方法:
在DOS“命令提示符”窗口中输入“ipconfig /all”,可以查看本机的详细信息。


import java.sql.*;public class EmpDAO {public static void main(String[] args){        // 驱动程序名        String driver = "com.mysql.jdbc.Driver";        // URL指向要访问的数据库名scutcs        String url = "jdbc:mysql://192.168.1.3:3306/emp";        // MySQL配置时的用户名        String user = "root";        // MySQL配置时的密码        String password = "123";        try {         // 加载驱动程序         Class.forName(driver);         // 连续数据库         Connection conn = DriverManager.getConnection(url, user, password);         if(!conn.isClosed())          System.out.println("Succeeded connecting to the Database!");         // statement用来执行SQL语句         Statement statement = conn.createStatement();         // 要执行的SQL语句         String sql = "select * from mytable";         // 结果集         ResultSet rs = statement.executeQuery(sql);         System.out.println("-----------------");                 String name = null;         String sex = null;         while(rs.next()) {          // 选择sname这列数据          name = rs.getString("name");          sex = rs.getString("sex");          // 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。          // 然后使用GB2312字符集解码指定的字节数组          name = new String(name.getBytes("ISO-8859-1"),"GB2312");          // 输出结果          System.out.println(name + "\t" + sex);         }         rs.close();         conn.close();        } catch(ClassNotFoundException e) {         System.out.println("Sorry,can`t find the Driver!");         e.printStackTrace();        } catch(SQLException e) {         e.printStackTrace();        } catch(Exception e) {         e.printStackTrace();        }}}

将数据库的连接和获取封装到DBUtility类中。


db.properties文件内容:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123
jdbc.url=jdbc\:mysql\://192.168.1.3\:3306/emp


import java.io.IOException;import java.sql.*;import java.util.Properties;import java.sql.Connection;public class DBUtility {private static Properties properties = new Properties();private static String driver = null; private static String url = null; private static String user = null;private static String pwd = null;static{try {properties.load(DBUtility.class.getClassLoader().getResourceAsStream(           "databaseDao/db.properties"));driver = properties.getProperty("jdbc.driver");url = properties.getProperty("jdbc.url");user = properties.getProperty("jdbc.user");pwd = properties.getProperty("jdbc.password");Class.forName(driver);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static Connection openConnection() throws SQLException{ return  DriverManager.getConnection(url, user, pwd);}public static void closeConnection(Connection con){if(con != null){try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("关闭连接时发生异常");}}}}

新的EmpDAO:

import java.sql.*;public class EmpDAO {public static void main(String[] args){EmpDAO dao = new EmpDAO();dao.findAll();}public void findAll(){Connection con = null;Statement stmt = null;ResultSet rs = null;try {con = DBUtility.openConnection();stmt = con.createStatement();rs = stmt.executeQuery("select name,sex from mytable");while(rs.next()){System.out.println(rs.getString("name")+ "," +rs.getString("sex"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(rs != null){     rs.close();}if(stmt != null){stmt.close();}} catch (SQLException e) {// TODO Auto-generated catch blockSystem.out.println("数据库释放资源异常");}DBUtility.closeConnection(con);}}}





0 0
原创粉丝点击