JAVA EE 串連MySql資料庫

來源:互聯網
上載者:User

標籤:jar   sys   寫入   creat   round   connector   desc   from   result   

  接上次JDBC 使用Derby資料庫連接之後 這次嘗試使用MySql 串連=-=

  我們得先下載Mysql的驅動包:mysql-connector-java-5.1.41-bin.jar

  下載之後 匯入:

完成這個之後我們開啟MySql Workbench 開啟本機資料庫 並建立 USERINFO 庫

然後再到index.jsp中寫入:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP ‘index.jsp‘ starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>    <body>    <%   //聲明Connection對象        Connection con;        //驅動程式名        String driver = "com.mysql.jdbc.Driver";        //URL指向要訪問的資料庫名mydata        String url = "jdbc:mysql://localhost:3306/USERINFO";        //MySQL配置時的使用者名稱        String user = "root";        //MySQL配置時的密碼        String password = "699307";        //遍曆查詢結果集         Connection conn = null;        try {            //載入驅動程式            Class.forName(driver);            //1.getConnection()方法,串連MySQL資料庫!!            conn = DriverManager.getConnection(url,user,password);            if(!conn.isClosed())                System.out.println("Succeeded connecting to the Database!");            //2.建立statement類對象,用來執行SQL語句!!            Statement st = conn.createStatement();                //執行sql語句         String sql = "select * from USERINFO";        //增刪改查表中的內容,用executeUpdate方法       st.executeUpdate("insert into USERINFO(USERID, USERNAME) values (1, ‘NOSTRING‘)");       st.executeUpdate("insert into USERINFO(USERID, USERNAME) values (2, ‘BECHAR‘)");       st.executeUpdate("insert into USERINFO(USERID, USERNAME) values (3, ‘ALEXVIEW‘)");               //讀取表的內容,用executeQuery方法        ResultSet rs = st.executeQuery("select * from USERINFO");        //迴圈輸出內容        while(rs.next()){            int userID = rs.getInt(1);            String username = rs.getString(2);            out.println("-------------------<br>");            out.println("uerid:"+ userID +"<br>");            out.println("username:"+ username +"<br>");            out.println("-------------------<br>");        }            }catch(SQLException e){        out.print(e.getErrorCode() + "<br>");        out.print(e.getCause() + "<br>");        out.print(e.getMessage() + "<br>");        if(conn != null )            conn.close();    } %>  </body></html>

寫入之後日常訪問:

JAVA EE 串連MySql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.