⑴ java中執行sql插入語句怎麼弄
1、Connection conn = DriverManager.getConnection(URL,資料庫登錄名,資料庫登錄密碼);//獲得資料庫連接。
2、Statement statement = con.createStatement(); //訪問資料庫。
3、ResultSet resultSet = statement.executeQuery(sql);//執行SQL語句。
⑵ java如何執行sql語句
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class xxxx {
public static void main(String[] args) {
Connection con = null ;
Statement stmt = null ;
try {
Class.forName("com.mysql.jdbc.Driver"); //mysql為例 不一樣的資料庫所需的驅動包不一樣 連接語句略有不同
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/資料庫名", "root", "密碼");
stmt = con.createStatement();
String sql = "insert into info values ('用戶', 'mima', 'piapiapia~')";
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}