Connection to mysql through java Learning

Source: Internet
Author: User
Java learning connection mysql first to add mysql-connector-java-5.1.10-bin.ja to the system java project file download. csdn. netdetailu0141125847359185Mysql ----- right-click Select Properties --------------- AddExternalJARS test example importjava. SQL. Con

Java learning connection mysql first Add mysql-connector-java-5.1.10-bin.ja to the system java project file http://download.csdn.net/detail/u014112584/7359185 Mysql ----- right-click Select Properties --------------- Add External JARS test example import java. SQL. Con

Connection to mysql through java Learning

First, add the mysql-connector-java-5.1.10-bin.ja to the system java project file

Http://download.csdn.net/detail/u014112584/7359185

Mysql -----> right-click and select Properties ---------------> Add External JARS





Test example


Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import com. mysql. jdbc. preparedStatement; import com. mysql. jdbc. statement; public class MysqlTest {static String drivername = "com. mysql. jdbc. driver "; static String url =" jdbc: mysql: // localhost: 3306/expression "; // point to the data source static String username =" root "; static String password = ""; static java. SQL. st Atement stmt = null; static ResultSet re = null; static Connection conn = null; static PreparedStatement pstm = null;/** constructor initialization */public MysqlTest () {try {Class. forName (drivername); // load the driver to the running environment. When loading, the driver automatically registers the System with DriverManager. out. println ("successfully created driver");} catch (ClassNotFoundException e) {e. printStackTrace () ;}}/** get Connection */public static Connection getConnection () {conn = null; try {conn = (Connection) DriverManager. getConnection (url, username, password); // after the driver and connection address are available, you need to use DriverManager to obtain the connection System. out. println ("connection succeeded! ");} Catch (SQLException e) {e. printStackTrace ();} return conn;}/*** close Connection * @ param args */public static void free (ResultSet rs, Connection conn, java. SQL. statement stmt2) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {// TODO Auto-generated catch blockSystem. out. println ("disabling ResultSet failed! "); E. printStackTrace () ;}finally {if (conn! = Null) {try {conn. close ();} catch (SQLException e) {// TODO Auto-generated catch blockSystem. out. println ("An error occurred while closing the Connection! "); E. printStackTrace () ;}finally {if (stmt2! = Null) {try {stmt2.close ();} catch (SQLException e) {// TODO Auto-generated catch block System. out. println ("disabling Statement failed! "); E. printStackTrace () ;}}}}} public static void main (String [] args) {MysqlTest. getConnection (); try {stmt = conn. createStatement ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} try restarting stmt.exe cuteQuery ("select * from data");} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} int I = 1; try {while (re. next () {System. out. println (I ++) ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} free (re, conn, stmt); System. out. println ("OK ");}}

More java Database Connection

Http://download.csdn.net/detail/u014112584/7359179

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.