原文鏈接隨便找的還行網(wǎng)頁鏈接

創(chuàng)新互聯(lián)建站是專業(yè)的任縣網(wǎng)站建設(shè)公司,任縣接單;提供成都做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行任縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
右鍵點擊項目名依次點擊new–Directory 創(chuàng)建文件夾lib
2.把mysql-connector-java-5.1.48-bin.jar包粘貼到lib目錄中
3.把數(shù)據(jù)庫連接jar包導(dǎo)入到項目中
JDBC步驟:
加載數(shù)據(jù)庫的驅(qū)動,它是java和數(shù)據(jù)庫之間的橋梁
2.獲取Connection,java和數(shù)據(jù)庫的一次連接
3.創(chuàng)建Statement對象,由Connection產(chǎn)生,執(zhí)行sql語句
4.如果要接收返回值,創(chuàng)建ResultSet對象,保存Statement執(zhí)行后所查到的結(jié)果
增刪改代碼:
package cn.web.jdbc;
import java.sql.*;
public class executeUpdate {
public static void main(String[] args) {
//? 加載驅(qū)動
try {
Class.forName("com.mysql.jdbc.Driver");
// 獲取連接
String url = "jdbc:mysql://localhost:3306/usejdbc?useUnicode=truecharacterEncoding=UTF-8useSSL=false";
String user = "root";
String password = "123456";
try {
//? 連接對象輸入三個參數(shù)
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
//定義sql語句
// 增
String sql1 = "insert into student(username,password) values ('諸葛亮','111111')";
// 刪
String sql2 = "delete from student where username ='諸葛亮'";
// 改
String sql3 = "update student set? username='老八' where id = 1 ";
Statement statement = connection.createStatement();
//? ? ? ? ? ? ? ? 修改這里的sql即可
int count = statement.executeUpdate(sql1);
System.out.println(count);
//? ?----------------------------------------------------------------
//? ? ? ? ? ? ? ? 釋放資源
statement.close();
connection.close();
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
網(wǎng)上截取的一段改的代碼,包括連接mysql數(shù)據(jù)庫,希望能幫到你。!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Dao {
private Connection conn = null;
PreparedStatement statement = null;
void connSQL() {
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"; // 數(shù)據(jù)庫地址,端口,數(shù)據(jù)庫名稱,字符集
String username = "root"; // 數(shù)據(jù)庫用戶名
String password = "root"; // 數(shù)據(jù)庫密碼
try {
Class.forName("com.mysql.jdbc.Driver"); // 加載驅(qū)動,必須導(dǎo)入包mysql-connector-java-5.1.6-bin.jar
conn = DriverManager.getConnection(url, username, password);
}
// 捕獲加載驅(qū)動程序異常
catch (ClassNotFoundException cnfex) {
System.err.println("裝載 JDBC/ODBC 驅(qū)動程序失敗。");
cnfex.printStackTrace();
}
// 捕獲連接數(shù)據(jù)庫異常
catch (SQLException sqlex) {
System.err.println("無法連接數(shù)據(jù)庫");
sqlex.printStackTrace();
}
}
// 關(guān)閉數(shù)據(jù)庫
void deconnSQL() {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println("關(guān)閉數(shù)據(jù)庫異常:");
e.printStackTrace();
}
}
boolean updateSQL(String sql) {
try { ?
statement = conn.prepareStatement(sql); ?
statement.executeUpdate(); ?
return true; ?
} catch (SQLException e) { ?
System.out.println("更新數(shù)據(jù)庫時出錯:"); ?
e.printStackTrace(); ?
} catch (Exception e) { ?
System.out.println("更新時出錯:"); ?
e.printStackTrace(); ?
} ?
return false; ?
} ?
void print(ResultSet rs) {
System.out.println("-----------------"); ?
System.out.println("查詢結(jié)果:"); ?
System.out.println("-----------------"); ?
try { ?
while (rs.next()) { ?
System.out.println(rs.getInt(0) + "/t/t" + rs.getString(1) ?
+ "/t/t" + rs.getString(2)); ?
} ?
} catch (SQLException e) { ?
System.out.println("顯示時數(shù)據(jù)庫出錯。"); ?
e.printStackTrace(); ?
} catch (Exception e) { ?
System.out.println("顯示出錯。"); ?
e.printStackTrace(); ?
} ?
} ?
public static void main(String args[]) {
Dao dao = new Dao(); ?
dao.connSQL(); // 連接數(shù)據(jù)庫 ?
String s = "select * from users"; ?
String update = "update users set userPWD =20000 where userID= '10000'";
if (dao.updateSQL(update) == true) {
System.out.println("更新成功"); ?
ResultSet resultSet = dao.selectSQL(s); ?
dao.print(resultSet); ?
} ?
dao.deconnSQL(); // 關(guān)閉數(shù)據(jù)庫連接
} ?
} ?
來源地址:
代碼如下:
import?java.io.BufferedReader;
import?java.io.BufferedWriter;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.OutputStreamWriter;
import?java.util.ArrayList;
import?java.util.List;
public?class?App61?{
public?static?void?main(String[]?args)?throws?IOException?{
//?查找行輸出
String?line?=?findFileLine("mylist.txt",?"abc");
System.out.println(line);
//?刪除指定行
removeFileLine("mylist.txt",?2);
}
static?void?removeFileLine(String?file,?int?line)?throws?IOException?{
ListString?lines?=?readFileLines(file);
lines.remove(line?-?1);
FileOutputStream?outputStream?=?null;
OutputStreamWriter?streamWriter?=?null;
BufferedWriter?writer?=?null;
try?{
outputStream?=?new?FileOutputStream(file);
streamWriter?=?new?OutputStreamWriter(outputStream);
writer?=?new?BufferedWriter(streamWriter);
for?(String?str?:?lines)?{
writer.write(str?+?System.lineSeparator());
}
}?finally?{
if?(writer?!=?null)?{
writer.close();
}
if?(streamWriter?!=?null)?{
streamWriter.close();
}
if?(outputStream?!=?null)?{
outputStream.close();
}
}
}
//?查找行
static?String?findFileLine(String?file,?String?keywork)?throws?IOException?{
ListString?lines?=?readFileLines(file);
for(String?line?:?lines)?{
if?(line.contains(keywork))?{
return?line;
}
}
return?"";
}
//?返回文件所有行
static?ListString?readFileLines(String?file)?throws?IOException?{
ListString?lines?=?new?ArrayList();
FileInputStream?inputStream?=?null;
InputStreamReader?streamReader?=?null;
BufferedReader?reader?=?null;
try?{
inputStream?=?new?FileInputStream(file);
streamReader?=?new?InputStreamReader(inputStream);
reader?=?new?BufferedReader(streamReader);
String?line?=?"";
while?((line?=?reader.readLine())?!=?null)?{
lines.add(line);
}
}?finally?{
if?(reader?!=?null)?{
reader.close();
}
if?(streamReader?!=?null)?{
streamReader.close();
}
if?(inputStream?!=?null)?{
inputStream.close();
}
}
return?lines;
}
}
1、首先在電腦上啟動數(shù)據(jù)庫 ,在數(shù)據(jù)庫中創(chuàng)建表,下面給出具體的SQL語句。
2、然后打開eclipse 創(chuàng)建新項目 JDBCTest,需要導(dǎo)入相關(guān)的jar包并構(gòu)建路徑,如圖。
3、接著創(chuàng)建entity實體層如圖對應(yīng)表中的數(shù)據(jù)。
4、創(chuàng)建數(shù)據(jù)連接層conn 用于MySQL數(shù)據(jù)庫的連接代碼如圖 如圖。
5、創(chuàng)建dao層持久層,在里面編寫數(shù)據(jù)庫表的增刪改查的具體操作。
6、最后編寫測試類 Test代碼如圖,就完成了。
首先你得確定你的數(shù)據(jù)庫連接是通過什么形式連接的,hibernate還是原生態(tài)的jdbc 還是spring;
如果是只有hibernate,那么你得通過加載配置文件得到sessionFactory,然后得到session如果spring,那么同樣也需要注入sessionfactory到你的dao如果是jdbc方式,那么你就按照原生態(tài)jdbc寫法總之,在你構(gòu)造DAO時,得有數(shù)據(jù)源。這樣才能操縱你的數(shù)據(jù)庫
如果搞懂了這些問題,那么你的第一個,第三個問題就迎刃而解了。至于第二問題,我沒明白你什么意思!
代碼如下:
public class Main {
public static void main(String[] args) {
int[] a = new int[]{92, 87, 2, 3, 4, 6, 7, 8, 22, 9, 12, 16, 20, 66, 23};
findNum(a, 6);
findNum(a, 300);
}
private static void findNum(int[] a, int num) {
for (int i = 0; i a.length; i++) {
if (a[i] == num) {
System.out.println("在數(shù)組中找到了" + num + ",位于數(shù)組的" + i + "位置");
return;
}
}
System.out.println("數(shù)組中沒有" + num + "這個數(shù)字");
}
}
運行結(jié)果:
名稱欄目:java增刪該查代碼,java實現(xiàn)增刪改查
文章轉(zhuǎn)載:http://chinadenli.net/article14/dsehjge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、電子商務(wù)、動態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、Google、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)