连接在Java到mysql

问题描述 投票:-1回答:2

我在Java编程中非常初学者。我使用的Eclipse IDE给我写code.I创建一个项目,并希望连接到MySQL。起初,我安装apache 9,然后打开Eclipse,并创建我的项目,然后安装mysql 8.当我跑我的代码,“抛出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver”所示。我已经拍摄的图像,从我的项目。

https://www.imgurl.ir/uploads/r22886_.jpg

请帮我。谢谢。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" buffer="none"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"  %>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>

<html>
<head>
<title>First java web</title>
</head>
<body>

<%
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex)
{
    out.print(ex.toString());
}
catch(Exception e)
{
    out.print(e.toString());
}
%>

</body>
</html>
java mysql connector
2个回答
1
投票

请把你的Servlet JDBC连接或更好,但在代码中创建,处理数据库连接和数据操作,你得到你的数据后,数据库层。现在回答你的问题

import java.sql.*;  

class MysqlCon{  

public static void main(String args[]){  
  try{  
      Class.forName("com.mysql.jdbc.Driver");  
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");  

     //here mydb is database name, root is username and password  
     Statement stmt=con.createStatement();  
     ResultSet rs=stmt.executeQuery("select * from emp");  

     while(rs.next())  
         System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
         con.close();  
     } catch(Exception e) { 
      System.out.println(e);
    }  
  }  
}  

0
投票

看起来你不熟悉Servlets和模式,如MVC。我建议你看看一些教程,因为可扩展性是软件发展的一个非常重要的一点。你开始在你的榜样将导致spagehetti代码的方式,它不会是有趣的继续发展。作为一个提示:如果你想在java开发,您还可以看看春季工具套件(STS)框架。我让Java开发很容易,你有一些伟大的教程有一个快速启动。用Python瓶也开始与网络开发的好办法。

© www.soinside.com 2019 - 2024. All rights reserved.