1.Java连接MySQL数据库
Java连接MySql需要下载JDBC驱动MySQL-connector-java-5.0.5.zip(举例,现有新版本)。然后将其解压缩到任一目录。我是解压到D盘,然后将其目录下的MySQL-connector-java-5.0.5-bin.jar加到classpath里,具体如下:
“我的电脑”-> “属性” -> “高级” -> “环境变量”,在系统变量那里编辑classpath,将D:/MySQL-connector-java-5.0.5/MySQL-connector-java-5.0.5-bin.jar加到最后,在加这个字符串前要加“;”,以与前一个classpath区分开。然后确定。
package hqs;import java.sql.*;public class DataBasePractice { public static void main(String[] args) { //声明Connection对象 Connection con; //驱动程序名 String driver = "com.mysql.jdbc.Driver"; //URL指向要访问的数据库名mydata String url = "jdbc:mysql://localhost:3306/mydata"; //MySQL配置时的用户名 String user = "root"; //MySQL配置时的密码 String password = "root"; //遍历查询结果集 try { //加载驱动程序 Class.forName(driver); //1.getConnection()方法,连接MySQL数据库!! con = DriverManager.getConnection(url,user,password); if(!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); //2.创建statement类对象,用来执行SQL语句!! Statement statement = con.createStatement(); //要执行的SQL语句 String sql = "select * from student"; //3.ResultSet类,用来存放获取的结果集!! ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("执行结果如下所示:"); System.out.println("-----------------"); System.out.println(" 学号" + "/t" + " 姓名"); System.out.println("-----------------"); String name = null; String id = null; while(rs.next()){ //获取stuname这列数据 name = rs.getString("stuname"); //获取stuid这列数据 id = rs.getString("stuid"); //首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。 //然后使用GB2312字符集解码指定的字节数组。 name = new String(name.getBytes("ISO-8859-1"),"gb2312"); //输出结果 System.out.println(id + "/t" + name); } rs.close(); con.close(); } catch(ClassNotFoundException e) { //数据库驱动类异常处理 System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //数据库连接失败异常处理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ System.out.println("数据库数据成功获取!!"); } }}
2.添加、修改、删除操作
在上面while代码段后面添加以下代码段:
String name = null; String id = null; while(rs.next()){ //获取stuname这列数据 name = rs.getString("stuname"); //获取stuid这列数据 id = rs.getString("stuid"); //首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。 //然后使用GB2312字符集解码指定的字节数组。 name = new String(name.getBytes("ISO-8859-1"),"gb2312"); //输出结果 System.out.println(id + "/t" + name); } PreparedStatement psql; ResultSet res; //预处理添加数据,其中有两个参数--“?” psql = con.prepareStatement("insert into student values(?,?)"); psql.setInt(1, 8); //设置参数1,创建id为5的数据 psql.setString(2, "xiaogang"); //设置参数2,name 为小明 psql.executeUpdate(); //执行更新 //预处理更新(修改)数据 psql = con.prepareStatement("update student set stuname = ? where stuid = ?"); psql.setString(1,"xiaowang"); //设置参数1,将name改为王五 psql.setInt(2,10); //设置参数2,将id为2的数据做修改 psql.executeUpdate(); //预处理删除数据 psql = con.prepareStatement("delete from student where stuid = ?"); psql.setInt(1, 5); psql.executeUpdate(); //查询修改数据后student表中的数据 psql = con.prepareStatement("select*from student"); res = psql.executeQuery(); //执行预处理sql语句 System.out.println("执行增加、修改、删除后的数据"); while(res.next()){ name = res.getString("stuname"); id = res.getString("stuid"); name = new String(name.getBytes("ISO-8859-1"),"gb2312"); System.out.println(id + "/t" + name); } res.close(); psql.close();该代码段使用到了预处理语句:
con.prepareStatement(String sql);
这样生成数据库底层的内部命令,并将该命令封装在preparedStatement对象中,可以减轻数据库负担,提高访问数据库速度。
运行结果:

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。 whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

查看InMysqlareBeneForsImplifyingComplexqueries,增強安全性,確保dataConsistency,andOptimizingPerformance.1)他們simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。