當(dāng)前位置:
首頁(yè) >
连接数据库实例
發(fā)布時(shí)間:2025/3/11
23
豆豆
idea連接數(shù)據(jù)庫(kù)步驟詳解
package com.company; import java.sql.*; public class Main {public static void main(String[] args) {Student student = new Student();try {SqlOperation.main();ResultSet resultSet = ScoreSql.selectScoreSql();//輸出所有行操作while(resultSet.next()){System.out.println(resultSet.getString("name"));System.out.println(resultSet.getFloat("score"));}}catch(Exception e) {e.printStackTrace();System.out.println("連接數(shù)據(jù)庫(kù)失敗!");}}SqlOperation.close(); }連接數(shù)據(jù)庫(kù)并創(chuàng)建操作對(duì)象
package com.company;import java.sql.*;public class SqlOperation {public static Connection connection = null;//定義連接數(shù)據(jù)庫(kù)的對(duì)象(橋梁)public static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Studentinfo";public static Statement statement = null;//定義靜態(tài)操作對(duì)象public static PreparedStatement preparedStatement = null;//定義動(dòng)態(tài)操作對(duì)象public static void main() {try{//第一步加載驅(qū)動(dòng)Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");System.out.println("加載驅(qū)動(dòng)成功!");//第二步建立連接connection = DriverManager.getConnection(url,"sa","shejiashuai");System.out.println("連接數(shù)據(jù)庫(kù)成功!");//第三步建立操作對(duì)象statement = connection.createStatement();}catch (Exception e){e.printStackTrace();System.out.println("連接數(shù)據(jù)庫(kù)失敗!");}}public static void close(){//關(guān)閉對(duì)象try{statement.close();connection.close();}catch (Exception e){e.printStackTrace();}} } package com.company;import java.sql.ResultSet; //創(chuàng)建操作成績(jī)數(shù)據(jù)庫(kù)的類 class ScoreSql {public static ResultSet selectScoreSql()throws Exception{return SqlOperation.statement.executeQuery("select * from Score");//調(diào)用SqlOperation.statement并使用其函數(shù)}public static void insertScoreSql(Score score)throws Exception{SqlOperation.statement.executeUpdate("insert into Score values ("+score.getName()+","+score.getScore()+")");}public static void createScoreSql()throws Exception{SqlOperation.statement.executeUpdate("create table Score(name char(10),score float)");}public static void deleteScoreSql(String name)throws Exception{SqlOperation.statement.executeUpdate("delete from Score where name = " +name);}public static void updateScoreSql(Score score)throws Exception{SqlOperation.statement.executeUpdate("update Score set score = " +score.getScore() + "where name = " +score.getName());}public static void statisticScoreSql(float score)throws Exception{//統(tǒng)計(jì)大于此分?jǐn)?shù)的人SqlOperation.statement.executeUpdate("select * from Score where score > " +score);} } //下面是成績(jī)類可省略不看 public class Score{String name;float score;public void setName(String name) {this.name = name;}public void setScore(float score) {this.score = score;}public String getName() {return name;}public float getScore() {return score;} } package com.company;import java.sql.ResultSet;class StudentSql{public static ResultSet selectStudentSql()throws Exception{return SqlOperation.statement.executeQuery("select * from Student where age > 18");} } //下面是學(xué)生類可省略不看 public class Student{private String name;private String sex;private String age;public String getName() {return name;}public String getSex() {return sex;}public String getAge() {return age;}public void setName(String name) {this.name = name;}public void setSex(String sex) {this.sex = sex;}public void setAge(String age) {this.age = age;} }總結(jié)
- 上一篇: 图灵奖演讲稿
- 下一篇: python-视频分帧多帧合成视频