JDBC的一点东西

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cjxtw/article/details/83113876

1:首先要获得与数据库的连接,connection接口可以创建Statement(用来执行查询和其他操作),查询得到的结果封装在ResultSet中,ResultSet接口提供对结果的一些处理方法(遍历等)。

2:因为connection通过其实现类来创建连接一次只能获取一种数据库的连接,没有通用性。所以抽象一个Driver接口来屏蔽连接的细节。

3:Driver 不需要new,每个数据库的Driver都用文字字符串来表示,利用反射机制,在运行时动态去装载。

4:当同一个程序需要访问多个数据库时,多个Driver会被动态加载进来,如何管理是个问题。这时需要DriverManager来管理。

当不同数据库的Driver被初始化时就将Driver注册到DriverManager中,用的是DriverManager.registerDriver方法(写在静态代码块中,使Driver实现类被装载时就注册)。

5:DriverManager提供一个getConnection的方法类建立数据库,在建立之间需要当Driver实现类的acceptURL来判断是不是知己支持的数据库,如果是就建立连接,不是就放弃。

这样就实现了最终的JDBC

class.forName(com.mysql.jdbc.Driver); //Driver的类名,代表要连接的数据库

String url = "jdbc:mysql://192.168.1.1:3306/****"  //连接的地址,包括端口号和数据库类型

Connection con = DriverMagger.getConnection(url, userName, password);  //获得连接

 

展开阅读全文

一点有用的东西

09-27

Imports System.DrawingrnrnPublic Class Form1rnrn Public Structure StaticValuern Public Value As Integerrn Public CurrentTime As Stringrn End Structurernrn 'Public Structure CPUStaticsrn ' Public MinY As Integerrn ' Public MaxY As Integerrn Public CriticalLine As Integerrn Public StaticValues As new ArrayListrn Public RefreshRate As Integerrnrn 'End Structurernrn ' Private CPUStatic As CPUStaticsrn Public Const UNITDISTANCE = 10rn Public Const UNITPOINTS = 4rn Public CPUBitmap As Bitmaprn Public g As Drawing.Graphicsrn Public rdm As New Random()rnrn Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loadrn CriticalLine = 90rn RefreshRate = 1000rn Timer1.Interval = RefreshRatern Timer1.Enabled = Truern DrawCurve()rn End Subrnrn Sub DrawCurve()rn Dim CPUPoints() As PointFrn If StaticValues.Count <= 0 Then Exit Subrnrn ReDim CPUPoints(StaticValues.Count - 1)rn Dim TotalLength As Integer = PictureBox1.Width / UNITPOINTSrnrn If TotalLength > CPUPoints.Length Thenrn For i As Integer = TotalLength - CPUPoints.Length To TotalLength - 1rn CPUPoints(i - (TotalLength - CPUPoints.Length)).X = UNITPOINTS * irn CPUPoints(i - (TotalLength - CPUPoints.Length)).Y = PictureBox1.Height - StaticValues(i - (TotalLength - CPUPoints.Length)).Value * PictureBox1.Height / 100rn Nextrn Elsern For i As Integer = 0 To TotalLength - 1rn CPUPoints(i - (TotalLength - CPUPoints.Length)).X = UNITPOINTS * irn CPUPoints(i - (TotalLength - CPUPoints.Length)).Y = PictureBox1.Height - StaticValues(i - (TotalLength - CPUPoints.Length)).Value * PictureBox1.Height / 100rn Nextrn End Ifrnrn g.Clear(Color.Black)rn g.DrawLine(Pens.LightGreen, 0, PictureBox1.Height Mod UNITDISTANCE, _rn PictureBox1.Width, PictureBox1.Height Mod UNITDISTANCE)rn For i As Integer = PictureBox1.Height Mod UNITDISTANCE To PictureBox1.Height Step UNITDISTANCErn g.DrawLine(Pens.DarkGreen, 0, i, PictureBox1.Width, i)rn Nextrnrn For i As Integer = UNITDISTANCE To PictureBox1.Width Step UNITDISTANCErn g.DrawLine(Pens.DarkGreen, i, 0, i, PictureBox1.Height)rn Nextrnrn 'draw critical line using RED colorrn Dim CriticalY As Integer = PictureBox1.Height - PictureBox1.Height * CriticalLine / 100rn g.DrawLine(New Pen(Color.Crimson, 2.0), 0, CriticalY, PictureBox1.Width, CriticalY)rnrn If Not CPUPoints Is Nothing Thenrn If CPUPoints.Length > 1 Thenrn g.DrawCurve(Pens.LightYellow, CPUPoints)rn End Ifrn End Ifrnrn PictureBox1.Refresh()rnrn End Subrnrn Sub AddCPUVaue(ByVal CPUValue As StaticValue)rn Dim tempValue As New StaticValuern tempValue = CPUValuern StaticValues.Add(tempValue)rn '假设每秒产生一次数据,一天最多产生86400个rn '这里只保存一天的数据rn If StaticValues.Count > 86400 Thenrn StaticValues.RemoveAt(0)rn End Ifrn End Subrnrn Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tickrn Dim CPUValue As StaticValuern CPUValue.Value = rdm.Next(50, 100)rn CPUValue.CurrentTime = "1"rn AddCPUVaue(CPUValue)rn 'Debug.Print(CPUValue.Value)rn DrawCurve()rnrn End Subrnrn Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resizern If Not CPUBitmap Is Nothing Then CPUBitmap.Dispose()rn CPUBitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)rn g = Drawing.Graphics.FromImage(CPUBitmap)rn PictureBox1.Image = CPUBitmaprn PictureBox1.Refresh()rnrn End SubrnEnd Classrn 论坛

Java的JDBC的一点小问题

05-21

public class ExecuteDDl rn private String driver;rn private String url;rn private String user;rn private String pass;rn rn public void initParam(String paramFile)throws Exceptionrn //使用Properties类来加载属性文件rn Properties props = new Properties();rn props.load(new FileInputStream(paramFile));rn driver = props.getProperty("com.mysql.cj.jdbc.Driver");rn url = props.getProperty("jdbc:mysql://localhost:3306/localhost");rn user = props.getProperty("root");rn pass = props.getProperty("asd857448959");rn rn public void createTable(String sql)throws Exceptionrn //加载驱动rn Class.forName(driver);rn try(rn //获取数据库链接rn Connection conn = DriverManager.getConnection("url", "user", "pass");rn //使用Connection来创建一个 Statement对象rn Statement stmt = conn.createStatement())rn rn //执行DDL语句,创建数据表rn stmt.executeUpdate(sql);rn rn rn rn public static void main(String[] args)throws Exceptionrn ExecuteDDl ed = new ExecuteDDl();rn ed.initParam("mysql.ini");rn ed.createTable("create table jdbc_test"rn +"( jdbc_id int auto_increment primary key,"rn +"jdbc_name varchar(255),"rn + "jdbc_desc text);");rn System.out.println("-----建表成功--------");rn rnrnrnrnrn当这代码执行产生一下错误rnException in thread "main" java.lang.NullPointerExceptionrn at java.base/java.lang.Class.forName0(Native Method)rn at java.base/java.lang.Class.forName(Unknown Source)rn at JDBCno1.ExecuteDDl.createTable(ExecuteDDl.java:26)rn at JDBCno1.ExecuteDDl.main(ExecuteDDl.java:41)rn麻烦各位小哥哥小姐姐了,这个弄了挺久就是不知道咋办~~~ 论坛

没有更多推荐了,返回首页