Java learning connection Mysql first import mysql-connector-java-5.1.10-bin.ja: download. csdn. netdetailu0141125847359185 mouse on the project, right-click Select Properties ----- JavaBuildPath ------ AddExternalJARS test code: importjava. SQL. Connec
Java learning connection Mysql first import mysql-connector-java-5.1.10-bin.ja: http://download.csdn.net/detail/u014112584/7359185 mouse over the project, right-click Select Properties ----- Java Build Path ------ Add External JARS test code: import java. SQL. Connec
Connection to Mysql through java Learning
Import mysql-connector-java-5.1.10-bin.ja first
: Http://download.csdn.net/detail/u014112584/7359185
Place the cursor over the project, right-click and choose Properties -----> Java Build Path ------> Add External JARS
Test code:
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 ");}}
Java Access database more examples http://download.csdn.net/detail/u014112584/7359179