本文目錄一覽:
- 1、java連接數據庫mysql代碼及簡單訪問數據庫
- 2、jdbc連接數據庫的代碼問題jdbc連接mysql數據庫
- 3、JAVA 使用JDBC連接MYSQL數據庫時,連接不同數據庫的方法
- 4、jsp中使用JDBC連接MySQL數據庫如何解決
java連接數據庫mysql代碼及簡單訪問數據庫
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(“數據庫數據成功獲取!!”);
}
}
}
在上面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();
jdbc連接數據庫的代碼問題jdbc連接mysql數據庫
用這個類吧.好的話,給我加加分.
import java.sql.*;
/**
* @功能: 一個JDBC的本地化API連接類,封裝了數據操作方法,只用傳一個SQL語句即可
* @作者: 李開歡
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 這裡可以將常量全部放入另一個類中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;
private static final String URL = “jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb”;
private static final String USER =”sa”;
private static final String PASS = “sa”;
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println(“連接中…”);
try {
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println(“成功連接”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println(“執行SQL語句中…”);
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals(“select”)){
rs = ps.executeQuery(sql);
System.out.println(“執行完查詢操作,結果已返回ResultSet集合”);
}else if(sql.substring(0, 6).equals(“delete”)){
ps.executeUpdate(sql);
System.out.println(“已執行完畢刪除操作”);
}else if(sql.substring(0, 6).equals(“insert”)){
ps.executeUpdate(sql);
System.out.println(“已執行完畢增加操作”);
}else{
ps.executeUpdate(sql);
System.out.println(“已執行完畢更新操作”);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println(“查詢結果為:”);
return rs;
}
public static void closeConnection(){
System.out.println(“關閉連接中…”);
try {
if (rs != null) {
rs.close();
System.out.println(“已關閉ResultSet”);
}
if (ps != null) {
ps.close();
System.out.println(“已關閉Statement”);
}
if (conn != null) {
conn.close();
System.out.println(“已關閉Connection”);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = “delete from type where id = 1”;
ConnectionDemo.getStatement(sql);
String sql2 = “insert into type values(1,’教學設備’)”;
ConnectionDemo.getStatement(sql2);
String sql1 = “select * from type”;
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println(“編號 “+”類 型”);
try {
while(rs.next()){
System.out.print(” “+rs.getInt(1)+” “);
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}
JAVA 使用JDBC連接MYSQL數據庫時,連接不同數據庫的方法
一般的話,一個數據庫連接用完之後是要關閉的。
如果是一個項目的話一般使用數據庫連接池,如果有多個數據庫的話最好是建立多個連接池,這樣的話,在系統啟動時,一次加載一定數量的連接對象,用完之後放回去。
如果你僅僅需要兩個數據庫連接對象的話,不如分別創建兩個靜態全局變量來保存兩個數據庫的連接對象。
jsp中使用JDBC連接MySQL數據庫如何解決
在index.jsp中輸入如下代碼,並配置相應mySQL數據庫數據
%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%
%@ page import=”java.sql.*” %
body
%
String driver = “com.mysql.jdbc.Driver”;
// URL指向要訪問的數據庫名test1
String url = “jdbc:mysql://127.0.0.1:3306/test”;
// MySQL配置時的用戶名
String user = “root”;
// Java連接MySQL配置時的密碼
String password = “111”;
try {
// 1 加載驅動程序
Class.forName(driver);
// 2 連接數據庫
Connection conn = DriverManager.getConnection(url, user, password);
// 3 用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = “select * from login”;
ResultSet rs = statement.executeQuery(sql);
String name = null;
String mima=null;
while (rs.next()) {
name = rs.getString(“userName”);
mima = rs.getString(“passWord”);
out.println(name+”\t”+mima);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println(“Sorry,can`t find the Driver!”);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
%
/body
12,這樣就運行成功了,對於出現8080端口號被佔用,可以採用如下的方法進行刪除對應的進程。
在命令提示符下,輸入netstat -aon | findstr 8080
找到對應的進程的PID,假設是7659 再輸入如下的命令
taskkill /pid 7659 /F
即可刪除對應的進程。
原創文章,作者:LRR16,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129470.html