jdbc串連mysql,jdbcmysql

來源:互聯網
上載者:User

jdbc串連mysql,jdbcmysql

Test.java


import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Connection;import java.sql.Statement;public class Test {static Connection con; // 聲明Connection對象static Statement sql; // 聲明Statement對象static ResultSet res;public Connection getConnection() { // 串連資料庫方法try {  Class.forName( "org.gjt.mm.mysql.Driver" );con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root","0000");} catch (Exception e) {e.printStackTrace();}return con; // 返回Connection對象}// ///////////////public static void insertdata() {Test c = new Test();con = c.getConnection();try {sql = con.createStatement();sql.executeUpdate("  insert  into  stu  values(12,'Shara' ) ;");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
Main.java

public class Main {public static void main(String[] args) {// TODO Auto-generated method stubTest.insertdata();System.out.println("finish");}}

輸出 : finish



記得要載入jar別忘了




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

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.