JDBC (Java Data Base Connectivity, Java Database Connectivity) is a Java API for executing SQL statements. It can provide unified access to multiple relational databases. It consists of a set of classes and interfaces written in the Java language. JDBC provides a benchmark that allows more advanced tools and interfaces to enable database developers to write database applications. JDBC cannot directly access the database, and needs to rely on the JDBC driver provided by the database manufacturer.
Database connection
If you want to access a database in Java, you must first load a database driver, which only needs to be loaded once on the first access. Then create a Connection instance every time you access the database to get the database connection, so that you can execute SQL statements that operate on the database. Finally, release the database connection after use.
Database driver class
Different databases implement different JDBC interfaces, so different database driver packages are generated. The driver package contains some classes responsible for database connections, and passes the SQL statements we want to operate into. My PC uses SQL2012, so we have to go here http://www.microsoft.com/zh-cn/search/DownloadResults.aspx?q=jdbc download driver
After downloading, import the driver package in the newly created java_project
Right-click to select the project >>Build Path >>Add External Archives... Select Download the unzipped file
Projects after successful import:
package com.Project_DataBase01;import java.sql.Connection;import java.sql.DriverManager;public class SelectQuery { private Connection conn; /* * Create a method that returns Connection*/ public Connection getConnection(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("Database driver loading successfully"); conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=java_conn_test","sa","123456"); if(conn==null){ System.out.println("Database connection failed"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- e.printStackTrace(); } return conn; }} Perform tb_User in the SqlServe database java_conn_test to add, delete, modify and check the data.
package com.Project_DataBase01;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class StartMain { private static Connection conn; public static void main(String[] args) { // TODO Auto-generated method stub conn=new SelectQuery().getConnection(); GetInsert(); GetSelect(); GetUpdate(); GetSelect(); GetDelete(); GetSelect(); } /* * INSERT */ public static void GetInsert(){ if(conn!=null){ //INSERT System.out.println("-----------INSERT------------"); int x=1+(int)(Math.random()*5000); String insert_str="INSERT INTO tb_User (UserName,UserPwd,UserId) VALUES ('name_"+x+"','pwd_"+x+"',NEWID())"; try { Statement insertstatement=conn.createStatement(); int result= insertstatement.executeUpdate(insert_str); if(result>0){ System.out.println("Address successfully"); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- try { PreparedStatement selectps=conn.prepareStatement(select_str); ResultSet rs=selectps.executeQuery(); while (rs.next()) { String name=rs.getString("UserName"); String pwd=rs.getString("UserPwd"); String UserId=rs.getString("UserId"); System.out.println(name+"/t"+pwd+"/t"+UserId); } System.out.println("Query successful"); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("-----------INSERT------------"); String update_str="UPDATE tb_User SET UserPwd=UserPwd+'xxxxxxxx' WHERE UserId='fa562573-218a-4205-b67d-ebdfac3f8329'"; try { Statement updatestatement=conn.createStatement(); int result=updatestatement.executeUpdate(update_str); if(result>0){ System.out.println("修改成功! "); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("-----------------------"); } } /* * DELETE */ public static void GetDelete(){ if(conn!=null){ //DELETE System.out.println("-----------DELETE------------"); String delete_str="DELETE tb_User WHERE UserId!='fa562573-218a-4205-b67d-ebdfac3f8329'"; try { Statement deletestatement=conn.createStatement(); int result=deletestatement.executeUpdate(delete_str); if(result>0){ System.out.println("Delete successful!"); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ } } else { System.out.println("Please check database connection"); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Run the program:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.