在Java里连接MySQL数据库。Connecting to a MySQL Database in Java
|
zhaoxq
老紫竹 ![]()
管理员
|
1 # 大 中 小 发表于 2008-05-03 06:51:06
In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end. So, we should know how we can manipulate the data in the database with the help of java, instead of going to database for a manipulation. We have many database provided like Oracle, MySQL etc. We are using MySQL for developing this application.
Java已经提供了一些类和API来让我们使用数据库。数据库在我们的程序中扮演了非常重要的角色,因为我们要在后台存储这些数据。我们需要了解如何通过Java的帮组来维护这些数据,而不是直接到数据库进行维护。我们有许多数据库,比如 Oracle, MySQL 等。我们用MySQl来开发这个应用。 In this section, you will learn how to connect the MySQL database with the Java file. Firstly, we need to establish a connection between MySQL and Java files with the help of MySQL driver . Now we will make our account in MySQL database so that we can get connected to the database. After establishing a connection we can access or retrieve data form MySQL database. We are going to make a program on connecting to a MySQL database, after going through this program you will be able to establish a connection on your own PC. 这一部分,我们将学习如何通过Java文件来连接MySQL数据库。第一步,我们使用MySQL的驱动建立MySQL和Java之间的连接。现在,我们有了MySQL的帐号来获得数据库连接。连接建立之后,我们可以从MySQL数据库获取数据。我们将创建一个程序来连接MySQL数据库,然后通过这个程序可以在我们自己的PC上建立一个链接。 Description of program: 程序描述 This program establishes the connection between MySQL database and java files with the help of various types of APIs interfaces and methods. If connection is established then it shows "Connected to the database" otherwise it will displays a message "Disconnected from database". 这个程序通过各种API的接口类型和方法建立了MySQL数据库与Java程序的链接,如果连接建立,将显示【Connected to database], 否则显示【Disconnected from database] Description of code: 代码描述 Connection: This is an interface in java.sql package that specifies connection with specific database like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of the Connection interface. 这个是java.sql的一个接口。Statement 都是在连接接口的上下文中执行的。 Class.forName(String driver): This method is static. It attempts to load the class and returns class instance and takes string type value (driver) after that matches class with given string. 这个是一个静态方法,他尝试装载类并返回类的实例。他使用驱动的类型作为参数,匹配对应的类 DriverManager: It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be register with this class. getConnection(String url, String userName, String password): This method establishes a connection to specified database url. It takes three string types of arguments like: url: - Database url where stored or created your database userName: - User name of MySQL password: -Password of MySQL con.close(): This method is used for disconnecting the connection. It frees all the resources occupied by the database. printStackTrace(): The method is used to show error messages. If the connection is not established then exception is thrown and print the message. Here is the code of program:
import java.sql.*;
public class RetriveAllEmployees {
public static void main(String[] args) {
System.out.println("Getting All Rows from employee table!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "jdbc";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM employee");
System.out.println("Employee Name: ");
while (res.next()) {
String employeeName = res.getString("employee_name");
System.out.println(employeeName);
}
con.close();
} catch (ClassNotFoundException e) {
System.err.println("Could not load JDBC driver");
System.out.println("Exception: " + e);
e.printStackTrace();
} catch (SQLException ex) {
System.err.println("SQLException information");
while (ex != null) {
System.err.println("Error msg: " + ex.getMessage());
System.err.println("SQLSTATE: " + ex.getSQLState());
System.err.println("Error code: " + ex.getErrorCode());
ex.printStackTrace();
ex = ex.getNextException(); // For drivers that support chained exceptions
}
}
}
}
快乐渡过每一天,减肥坚持每一天
|
|||||
|




